I pass the pointer a function that updates it. However, when a function returns a pointer, it returns to the value that it had before the function was called.
Here is my code:
#include <stdio.h> #include <stdlib.h> static void func(char *pSrc) { int x; for ( x = 0; x < 10; x++ ) { *pSrc++; } printf("Pointer Within Function: %p\n", pSrc ); } int main(void) { char *pSrc = "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson."; printf("Pointer Value Before Function: %p\n", pSrc ); func(pSrc); printf("Pointer Value After Function: %p\n", pSrc ); return EXIT_SUCCESS; }
Here is the conclusion
Pointer Value Before Function: 0x100403050 Pointer Within Function: 0x10040305a Pointer Value After Function: 0x100403050
What I was expecting was the value after the function matched the function inside the function.
I tried switching to char **pSrc , but this did not have the desired effect.
I'm sure the answer is pretty simple, but I'm restoring a hardware engineer and don't seem to understand it :-)
c function pass-by-value pointers
asicman
source share