C Pointer to a pointer and follow a link

I am trying to learn C, and I'm a bit suspended from pointers to pointers. I think I understand why you need this, but I can not make my head wrap around what is happening.

For example, the following code does not work, as I would expect this:

#include <stdio.h> int newp(char **p) { char d = 'b'; *p = &d; /*printf("**p = %c\n", **p);*/ return 1; } int main() { char c = 'a'; char *p = &c; int result; result = newp(&p); printf("result = %d\n", result); printf("*p = %c\n", *p); printf("c = %c\n", c); return 0; } 

As a result, I get the following:

 result = 1 *p = c = a 

* p prints like nothing. Instead, I would expect *p = b .

However , if I uncomment line 6 ( printf in the newp function), I get the following:

 **p = b result = 1 *p = b c = a 

What am I missing?

+4
source share
4 answers

You are dealing with undefined behavior. The variable d is local (located on the stack) and is not available after returning the close function ( newp ).

When dereferencing p outside newp address on the &d stack may be overwritten by some other local variable or may contain garbage.

+7
source

You save the address of the local variable ( d ) to *p , and then cast it when the variable goes out of scope. Undefined Behavior

+6
source

You may have lost the static , as in:

 int newp(char **p) { static char d = 'b'; /* <--- HERE */ *p = &d; /*printf("**p = %c\n", **p);*/ return 1; } 

This static causes the compiler to find a local variable in "static memory" that continues to exist (that is, retains its value) for periods of time after calling newp() . However, there is only one copy of this memory - each subsequent call to the contained function ( newp ) reuses the same memory cell and can overwrite the value at this time.

Without the static , to qualify the declaration of a local variable, the store will be "automatically", which means that it is automatically unloaded from its current use after returning the contained function. After returning newp memory previously used for the local variable can be reused for any purpose.

+1
source
 #include <stdio.h> // Here *pointer means that the parameter that this function // will expect will be a pointer. void changeViaPointer(int *pointer); // Pointer of a pointer. void changeViaPointerInBetween(int **pointer); int main(){ int number; number = 20; // Here *pointer means that the variable that is declared will be a pointer. int *pointer; // Actually asigning value to the pointer variable. pointer = &number; // Pointer of a pointer. int **pointerInBetween; // Assigning value to the pointer of a pointer. // Assigning the memory location where this pointer points to. // So this is a pointer in between. pointerInBetween = &pointer; printf("The number before changing is %d\n", number); // Pass the pointer variable. changeViaPointer(pointer); printf("The number after pointer changing is %d\n", number); // Pass the pointer of a pointer variable. changeViaPointerInBetween(pointerInBetween); printf("The number after pointer in between changing is %d\n", number); return 0; } void changeViaPointer(int *pointer){ // Okay, at this point we have received a variable called pointer, // which points to some value. In order to access this value // we need to use *pointer. // BUT THIS IS DIFFERENT FROM THE *pointer IN THE FUNCTION DECLARATION!!! *pointer = *pointer + 20; } void changeViaPointerInBetween(int **pointer){ // **pointer explanation: // Only pointer is the memory location // *pointer is the value of that memory location, which in this specific case is also a memory location // **pointer is the value of what the other pointer points to. **pointer = **pointer + 20; } 
0
source

All Articles