我正在向指针传递一个更新它的函数.但是当函数返回指针时,它返回到函数调用之前的值.
这是我的代码:
#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; }
这是输出
Pointer Value Before Function: 0x100403050 Pointer Within Function: 0x10040305a Pointer Value After Function: 0x100403050
我尝试切换到char ** pSrc,但没有达到预期的效果.
我相信答案很简单,但我是一名正在恢复的硬件工程师,似乎无法弄清楚:-)