一篇blog文章的错误: c语言指针类型参数的传递方式
http://blog.csdn.net/darz/archive/2006/12/17/1446992.aspx
void set(char *p){
p=NULL;
printf( "address of p is %d\n ",(signed)p);
}
void main()
{
char *str,a= 'A ';
str=&a;
printf( "before call set(),the value of str is %d,value of *str is %c ",(unsigned)str,*str);
set(str);
printf( "after call set(),the value of str is %d,value of *str is %c ",(unsigned)str,*str);
}
输出结果如下:
before call set(),the value of str is 1245048,value of *str is A
address of p is 0
after call set(),the value of str is 1245048,value of *str is A
由此可见,指针变量str的值在调用前后并没改变,所以它所指向的内容也不会变。
void set(char *p){
p=NULL;\\去掉这句试试
printf( "address of p is %d\n ",(signed)p);
}
void main()
{
char *str,a= 'A ';
str=&a;
printf( "before call set(),the value of str is %d,value of *str is %c ",(unsigned)str,*str);
set(str);
printf( "after call set(),the value of str is %d,value of *str is %c ",(unsigned)str,*str);
}
before call set(),the value of str is 1245048,value of *str is A
address of p is 1245048
after call set(),the value of str is 1245048,value of *str is A
这个作者写错了对吧
[解决办法]
你给分的话,那可能就是他错了。
[解决办法]
有功夫研究可信度很成问题的csdn blog文章,不如去看看权威书籍。
------解决方案--------------------
错误难免的..
[解决办法]
没写错,在set中访问的p是调用是被压到栈中的参数,对p进行修改实际上修改的是栈的内容,不影响作为实参的str。
//调用前str中存放的是变量a的地址1245048,该地址中存放字符 'A '
before call set(),the value of str is 1245048,value of *str is A
//调用时,str的内容(即地址1245048)被压入栈中
//进入set()后,p就是栈中存放1245048的地方
//此时p的内容是地址1245048,而通过*p可访问到该地址中的内容 'A '
//如果*p= 'B ',则可把地址1245048中的内容 'A '改成 'B '
//如果p=NULL,则是把栈中存放的1245048改成了0,但对存放在str中的地址1245048没有影响
address of p is 0
//set返回,str内容不变仍是1245048
after call set(),the value of str is 1245048,value of *str is A
[解决办法]
输出结果如下:
before call set(),the value of str is 1245048,value of *str is A
address of p is 0
after call set(),the value of str is 1245048,value of *str is A
由此可见,指针变量str的值在调用前后并没改变,所以它所指向的内容也不会变。
-------------------------------------
str没有变,这是因为给函数传递的是其拷贝,一个副本,所以在函数内对形参修改不会影响实参str。但这并不意味着其所指的内容不会被改变,如果先在没修改形参时对指针所指内存进行修改,那么,修改的就是str所指的内存,自然。。。。。str所指的内容被改变了。
所以说上面作者那句话说得不是很严密,我相信作者其实肯定是明白的。
[解决办法]
这个。。。
接分。