关于指针作为形参时,值是如何传递和改变的。
当调用函数向被调用函数传递一个指针值,而在被调用函数中对这个指针所表示的地址的值进行重新赋值将会改变调用函数中的值。
#include <stdio.h>
void foo(char *p)
{
p="world\n\0";
printf("%s",p);
}
void main()
{
char *p = "hello,world\n\0";
printf("%s",p);
foo(p);
}
编译并且输出:
xiaowen@hello-laptop:~/test/test-c$ gcc face2.c -o face2
xiaowen@hello-laptop:~/test/test-c$ ./face2
hello,world
world