Reputation: 23
I am new to C, and am trying to implement a stack with linked list. Currently set up the Stack, all good so far. The problem has come in when I try to push a new node onto the list. I currently have
In main()
, push()
is called by:
push(&(s.head), 'r');
The function push
is:
void push(StackNodePtr *topPtr, char value){
printf("topPtr value %c", (*topPtr)->data); // - Is currently 'p'
StackNodePtr sNP;
sNP = malloc(Node_Size);
sNP->data = value; // - Is currently 'r'
sNP->nextPtr = *topPtr;
printf("\nsNP value - %c", sNP->nextPtr->data); // Prints p... cool
topPtr = &sNP; // Just assigned it???
printf("\ntopPtr at end of push = %c", (*topPtr)->data); // prints r... cool
// WHY YOU NO REFERENCE sNP LATER!?!?
}
Meanwhile, back in main:
printf("\non the stack...%c", stackTop(s.head)); // prints 'p'
It seems to work fine in push, however I call printf()
on the node that topPtr
pointed to and the value that topPtr
used to be prints out instead (in this case 'p'). As far as I can tell from the hunting I've done, it looks and feels correct and I don't know what I have missed.
Could it be where I have done topPtr = &sNP;
?
Any "push" in the right direction is a good push...
Upvotes: 1
Views: 928
Reputation: 3118
Your function push is modifying the pointer topPtr. Since C is pass by value, the head is passed as a value and the copy passed gets modified in push. So, to modify the pointer itself, you need to pass a pointer to pointer. Also, the function push() signature needs to be corrected to pass in a pointer to pointer. Finally, the topPtr assignment in push needs to be corrected as shown in the code snippet.
Do the following changes:
push(&(s.head), 'r'); // pass pointer to pointer, assuming head is pointer, it should be fine
void push(StackNodePtr **topPtr, char value){ // make first argument pointer to pointer.
StackNodePtr sNP;
sNP = malloc(Node_Size);
sNP->data = value;
sNP->nextPtr = *topPtr;
*topPtr = &sNP; <------*topPtr needs to be assigned.
}
Upvotes: 0
Reputation: 477
its looks like the bug is here: topPtr = &sNP; // Just assigned it???
instead of push returning void. change it to return the new head of the stack. return sNp;
Upvotes: 0
Reputation: 9671
It should be
*topPtr = sNP;
this way, the original head, the caller has passed the pointer to, that became the next of the new head, is "overwritten" correctly, and the caller has the correct pointer to the new head.
Upvotes: 1
Reputation: 124702
topPtr = &sNP; // Just assigned it???
This assignment is not visible outside of the function. topPtr
is passed by value, i.e., a copy of it is made and passed to the function. So, assigning a different value only modifies the copy; the original argument remains pointing to the old memory location.
If you need to modify the argument in that way you need another level of indirection, i.e., take a StackNodePtr**
.
Also, I am assuming that StackNodePtr
is a typedef
for a StackNode*
. Am I right about that? Is there a good reason to typedef
this pointer type? Typically it just serves to complicate things. I would recommend typedef
'ing a pointer type only when it is truly an opaque type (i.e., a HANDLE
on Windows).
Upvotes: 2
Reputation: 5189
topPtr = &sNP; // Just assigned it???
No, you didn't. You assigned the value to local copy of pointer. You change the value of topPtr
itself, and it is does not go outside. Instead, you should write to location it points to:
*topPtr = sNP;
Upvotes: 0