我正在向指针传递一个更新它的函数.但是,当函数返回指针时,它返回到函数调用之前的值.
这是我的代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
这是输出
Pointer Value Before Function: 0x100403050
Pointer Within Function: …Run Code Online (Sandbox Code Playgroud)