季大哥,帮帮我!
先看下面的代码
#include <iostream.h>
#include <conio.h>
void Func1(char *str)
{
str = "Hello!";
}
void Func2(char * * str)
{
* str = "Hello!";
}
void Func3(char * & str)
{
str = "Hello!";
}
void main()
{
char *str1= "AAA" ;
Func1(str1);
cout<<str1<<endl; //str1等于AAA,看起来好象是指针传递,因为字符串本身是指针,所以是值传递
char *str2= "BBB" ;
Func2(&str2); //取地址
cout<<str2<<endl; //str2等于Hello!,看起来好象是指针的指针传递,因为字符串本身是指针,所以是指针传递
char *str3= "CCC" ;
Func3(str3);
cout<<str3<<endl; //str13等于Hello!,看起来好象是指针的引用传递,因为字符串本身是指针,所以是引用传递
}
为什么是指针传递而起不到的作用,难道指针在这里失效了吗?我猜Func1传递的只是指针的值,但是C++怎么区分是指针还是指针的值呢?