3

I have never been good at playing with pointers in C. But this time I would like to ask for your help to resolve my problems with pointers. I have a function here to push a value into a stack.

void StackPush(stackT *stackPtr, stackElementT element){ stackNodeT* node = (stackNodeT *) malloc(sizeof(stackNodeT)); if (node == NULL){ fprintf(stderr, "Malloc failed\n"); exit(1); } else { node->element = element; node->next = StackEmpty(stackPtr)? NULL : *stackPtr; *stackPtr = node; } } 

If I change the last line to stackPtr = &node; this function does not work. My question is why? What's the difference between *stackPtr = node; and stackPtr = &node; ?

Any help would be appreciated.

0

4 Answers 4

3

stackT *stackPtr defines stackPtr as a pointer to stackT. The caller of the function passes a stackT object to this function.

Now, *stackPtr = node; modifies the value pointed to by the pointer stackPtr whereas stackPtr = &node; modifies the local value of the pointer variable itself.

stackT *mystack = createStack(); //mystack points to an empty stack StackPush1(mystack, elem1);//stackpush1 uses *stackPtr = node; //mystack points to the node with elem1 StackPush2(mystack, elem2);//stackpush2 uses stackPtr = &node; //the function updates its local copy, not the passed variable //mystack still points to the elem1 //node with elem2 is not accessible and is a memory leak. 

lets say we have int k=4; if I enter something like *ptr = k; in the "main" body (not inside a function), the results should be the same as ptr = &k;?

Not exactly. Run the following code and see the difference for yourself:

int k = 4; //declare a pointer to int and initialize it int *ptr1 = malloc(sizeof(int)); //now ptr1 contains the address of a memory location in heap //store the current value into the address pointed to by ptr1 *ptr1 = k; /* this line will fail if we hadn't malloced in the previous line as it would try to write to some random location */ //declare a pointer to int int *ptr2; //and assign address of k to it ptr2 = &k; printf("Before \n*ptr1 = %d *ptr2 = %d\n", *ptr1, *ptr2); //change the value of k k = 5; printf("After \n*ptr1 = %d *ptr2 = %d\n", *ptr1, *ptr2); 

Post a comment if you need more clarification.

Sign up to request clarification or add additional context in comments.

1 Comment

lets say we have int k=4; if I enter something like *ptr = k; in the "main" body (not inside a function), the results should be the same as ptr = &k; ?
2
*stackPtr = node; 

This dereferences stackPtr and sets the object pointed at to the value of node.

stackPtr = &node; 

This changes the pointer stackPtr to point to the node pointer which is allocated on the stack.

Basically, in the first case, you are changing the thing that the pointer refers to (called the referent of the pointer) but the pointer itself remains unchanged. In the second case, you are changing the pointer to refer to something different.

Comments

1

Once stackPtr is passed in (by value), it is a local (automatic) variable, and modifying it doesn't affect any of the caller's automatic variables. However, when you do:

*stackPtr = node; 

you're modifying the object it points to.

Comments

0

*stackPtr = node

makes stackPtr point to what you've malloc'd.

*stackPtr = &node

makes stackPtr point to the address of a local variable, which will most likely be invalid once you return from this function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.