所谓的传值和引用

一直反反复复总是记不太清

今天总结:

如果你在调用的时候用了引用,对应的函数参数必须有*

如果你在调用的时候没有&,则如果你传的是指针,则对应函数要有参数要有*,如果想把参数本身传过去,要在函数定义的参数处加&

对于字符串而言和对于整型而言 cout<<a不一样的

void fun(int *a)
{
cout<<a+1<<endl;
}
int main()
{
int a=3;
fun(&a);
}

输出的是一个地址,参数是指针

void fun(int *a)
{
cout<<*a+1<<endl;//取内容
}
int main()
{
int a=3;
fun(&a);
}

。。。。。。。。。。。。。。。。。

void fun(int &a)
{
cout<<a+1<<endl;
}
int main()
{
int a=3;
fun(a);
}//4

参数是常量本身

。。。。。。。。。。。。。。。

void fun(char *a)
{
cout<<a+1<<endl;
}
int main()
{
char *str="fdsfds";
fun(str);
}//dsfds

...................

void fun(char *&a)
{
cout<<a+1<<endl;
}
int main()
{
char *str="fdsfds";
fun(str);
}一样的

。。。。。。。。。。。。。。。。

void fun(char *a)
{
cout<<*a<<endl;
}
int main()
{
char *str="afdsfds";
fun(str);
}//a

................................

。。。。。。。。。。。。。

void fun(char **a)
{
cout<<a+1<<endl;
}
int main()
{
char *str="fdsfds";
fun(&str);
}//一个地址

。。。。。。。。

void fun(char *a)
{
cout<<&a<<endl;
}
int main()
{
char *str="afdsfds";
fun(str);
}

指针的地址

。。。。。。。。。。。。。。。

void fun(char *a)
{
cout<<&(*a)<<endl;
}
int main()
{
char *str="afdsfds";
cout<<str;
fun(str);
}

两遍afdsfds

*a=a;a的地址是字符串首地址,相当于str

。。。。。。。。

void fun(char **a)
{
cout<<*a+1<<endl;
}
int main()
{
char *str="fdsfds";
fun(&str);
}//dsfds

原文地址:https://www.cnblogs.com/8335IT/p/5907719.html