I am passing a pointer a function that updates it. However when the function returns the pointer it returns to the value it had prior to the function call.
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 output
Pointer Value Before Function: 0x100403050
Pointer Within Function: 0x10040305a
Pointer Value After Function: 0x100403050
What I was expecting was the value after the function to match the one from within the function.
I tried switching to char **pSrc
but that did not have the desired affect.
I am sure the answer is fairly simple, but I am a recovering hardware engineer and can't seem to figure it out :-)
The pointer inside the function is a copy of the passed pointer.
They both hold the same address but have different addresses, so changing the address held by one of them doesn't affect the other.
If you want to increment the pointer inside the function pass it's address instead, like this
and
Also, be careful not to modify the contents, because your pointer points to a string literal, and string literals cannot be modified.
C
uses pass-by-value for function parameter passing. The value ofpSrc
is just a copy of thepSrc
present inmain()
, not the same entity as thepSrc
frommain()
. That is why, any changes made topSrc
inside the functionfunc()
won't be reflected inmain()
(caller function).However, all the changes to
*pSrc
will sustain.Solution: To change the
pSrc
ofmain()
fromfunc()
, you need to pass a pointer topSrc
(pointer to a pointer) frommain()
and adjust the data types accordingly. Also, in that case, please note, thepSrc
should be modifiable (present in read-write memory). As it is currently written, thepSrc
inmain()
is not modifiable.C is passing parameters by value, so it makes a copy of pSrc inside your function, and all the changes you've made are applied to a copy of pSrc. If you want to change the value of pSrc, then you should pass a pointer to pSrc, like this:
You call
func
by passingpSrc
. You think you are passing the same variable - That isfunc
will operate on the same memory location when it alterspSrc
. This is false.func
gets a copy ofpSrc
. The stack frame built in callingfunc
will have its ownpSrc
. What is modified is its version not the calling function's.To let
func
operate on the actual variable in main you got to pass address ofpSrc
-&pSrc
.Relevant concepts - Pass by Value and Pass by Reference.
When you give a pointer to a function, you have a copy of the reference value that you originally had.
Then, if you modify your pointer, you're modifying a copy.
If you want to update the original one, you need to pass it with a double pointer
**
which will let you modify the original pointer, passing a "reference to a reference" (double pointer).