Hi all, I'm fairly new to C programming and am currently trying to create a stack just using pointers.
I have created a function 'peek' that will print out all of the integers that are stored in the stack.
I want to pass the stack by value, so no changes are made to it outside of the function.
This doesn't work, and the program just crashes...as I'm using pointers, I'm guessing that I am missing something out to do with memory.
The code does work, however, when I pass the stack by reference...
Code:peek(stack,sizeof(stack));If anyone could either tell me what I am doing wrong, or point me in the right direction it would be greatly appreciated.Code:void peek(stack_t **stackptr, int size) { stack_t* peek_element; int i,data; for ( i=0; i < size; i++) { peek_element = (*stackptr); data = peek_element->data; (*stackptr) = peek_element->next; printf("%d ", data); } }
Thanks



LinkBack URL
About LinkBacks


