小编asi*_*man的帖子

更新函数中的指针

我正在向指针传递一个更新它的函数.但是,当函数返回指针时,它返回到函数调用之前的值.

这是我的代码:

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

c pointers function pass-by-value

7
推荐指数
1
解决办法
7010
查看次数

标签 统计

c ×1

function ×1

pass-by-value ×1

pointers ×1