void test(int *b)
{
*b = 10;
}
int main()
{
int a = 5;
test(&a);
printf("%d\n", a);
}
首先,看这个简单的函数,通过把实参变量a的地址传递给被调函数test,改变实参变量a的值。
总结下:
在传址中,主子函数关系为将实参的地址传给形参后,子函数的形参就能够操控主函数中的实参数值的变化,故形参的变化可以影响到实参。
在传值中,主子函数关系为将实参的数值传给形参后,主子函数的实参与形参就毫无关系,形参的任何改变均与实参无关。
例2:
void test_1(int b)
{
b = 10;
}
int main()
{
int a = 5;
test_1(a);
printf("%d\n", a);
}
这里需要说明的是在传址中,虽然传递的是地址,但是如果在被调函数中,只改变形参所指向的地址,不能改变原实参指向的地址(单向传递),这时,想改变实参的值也是不可能的。如:
例3:
void test(int *b)
{
int c = 10;
int *p = &c;
b = p;
}
int main()
{
int a = 5;
test(&a);
printf("%d\n", a);
}
好了,现在来看稍微复杂点的函数。
例4:
void _test(char*input)
{
printf("%lu\n", input);
input = (char *)malloc(strlen(input) +1);
printf("%lu\n", input); //3748176,随机的
input[0] = 'h';
input[1] = 'e';
input[2] = '\0';
}
int main()
{
char *s = "China";
_test(s);
printf("%s\n", s);
}
首先,看这个函数,本意是想通过被调函数_test()来改变实参s的值,即希望输出"he",但是,输出结果还是为China。
通过上面的分析,很明显这是传值,形参的改变不会影响到实参。如果要通过形参的改变影响到实参,必须要传址。
即&s。所以只能用指针的指针或指针的引用来改变。
例5:
void _test(char**input)
{
*input = (char *)malloc(10);
char *dst = *input;
dst[0] = 'h';
dst[1] = 'e';
dst[2] = '\0';
}
int main()
{
char *s = 0;
_test(&s);
printf("%s\n", s);
}
这样改过之后,输出的结果就是“he”了。来分析下,为什么二级指针就可以改变实参的内容,在被调函数里面,开辟了新的空间,是赋值给实参的地址的。而不像是前面那个函数,赋值给实参这个值。所以针对地址的改变会改变实参,而针对值的改变不会影响到实参。
在测试中,曾错误的写了下面的代码
例6:
void _test(char**input)
{
*input = (char *)malloc(10);
//char *dst = *input;
*input[0] = 'h';
*input[1] = 'e';
*input[2] = '\0';
}
int main()
{
char *s = 0;
_test(&s);
printf("%s\n", s);
}
最后的输出:
错误的原因是:**input是个二级指针,相当于行指针,*input[1]相当于下移了一行的位置,所以会出现访问错误。
改写成下面这样就正常了
例7:
void _test(char**input)
{
*input = (char *)malloc(10);
//char *dst = *input;
input[0][0] = 'h';
input[0][1] = 'e';
input[0][2] = '\0';
}
还有再改写的过程中,改写成下面这样为什么会出错
例8:
void _test(char**input)
{
input = (char **)malloc(10);
//char *dst = *input;
input[0][0] = 'h';
input[0][1] = 'e';
input[0][2] = '\0';
}
int main()
{
char *s = 0;
_test(&s);
printf("%s\n", s);
}
分析下:这里分配空间给input,注意和前面那个例子的区别,这时候赋值是赋给形参这个变量的,虽说实参传递给形参的时候是传址, 但是,在被调函数中,只是把形参给指向了其他内存单元,在其他内存单元的操作并不影响实参。
这里一定要跟*input =(char*)malloc(10)区别开,这里的内存分配是作用与实参地址的。可以类比下例4.
void
main()
{
char
|