Updating pointers in a function

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 :-)

+7
c function pass-by-value pointers
source share
5 answers

A pointer inside a 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 does not affect the other.

If you want to increase the pointer inside the function, go to this address, for example,

 static void func(char **pSrc) { int x; for ( x = 0; x < 10; x++ ) { (*pSrc)++; } printf("Pointer Within Function: %p\n", pSrc ); } 

and

 func(&pSrc); 

Also, be careful not to modify the contents, because your pointer points to a string literal, and string literals cannot be changed.

+7
source share

C uses a pass-by-value to pass parameters to a function. The pSrc value is just a copy of pSrc present in main() , and not the same object as pSrc from main() . Therefore, any changes made to pSrc inside the func() function will not be displayed in main() (caller function).

However, all changes to *pSrc will be supported.

Decision. To change pSrc of main() from func() , you need to pass a pointer to pSrc (pointer to a pointer) from main() and configure the data types accordingly. In addition, in this case, please note that pSrc must be modifiable (present in read / write memory). As it is currently written, pSrc in main() not pSrc .

+2
source share

When you point to a function pointer, you have a copy of the original value that you originally had.

Then, if you change your pointer, you change the copy .

If you want to update the original, you need to pass it using a double pointer ** , which will allow you to change the original pointer by passing a link to the link (double pointer).

+2
source share

C passes parameters by value, so it makes a copy of pSrc inside your function, and all the changes you make apply to the copy of pSrc. If you want to change the value of pSrc, then you must pass a pointer to pSrc, for example:

 static void func(char **pSrc){ // pointer to char* int x; for (x = 0; x < 10; x++) (*pSrc)++ } 
+1
source share

You call func by passing pSrc . You think you are passing the same variable. That is, func will work in the same memory location when pSrc changes. This is not true.

func gets a copy of pSrc . The stack frame created by calling func will have its own pSrc . Its version is changed, not the calling function.

In order for func to work with the actual variable in the main, you need to pass the address pSrc - &pSrc .

The relevant concepts are Pass by Value and Pass by Reference.

+1
source share

All Articles