65,176
社区成员




#include <iostream>
using namespace std;
void fun(char (&buff)[11])//括号里必须有11才不报错
{
cout<< buff;
}
int main(int argc, char *argv[])
{
char buff[] = "helloworld";
fun(buff);
return 0;
}
#include <iostream>
using namespace std;
void fun(char (&buff)[])//这样写报错,why? 写成void fun(char &buff[])也报错
{
cout<< buff;
}
int main(int argc, char *argv[])
{
char buff[] = "helloworld";
fun(buff);
return 0;
}
void fun(char *)
OR
template<int N>
void fun(char (&buff)[N])
{
// array size is N
}
template<size_t size>
void fun(char (&buff)[size])
{
cout<< buff;
}
void fun(char (&buff)[11])//括号里必须有11才不报错
{
cout<< buff << endl;
}
void fun2(char (*buff)[11])//括号里必须有11才不报错
{
cout<< *buff << endl;
}
int _tmain()
{
char buff[] = "helloworld";
fun(buff);
fun2(&buff);
return 0;
}
#include <iostream>
using namespace std;
void fun(char *&buff)//写成这样也有问题?
{
cout<< buff;
}
int main(int argc, char *argv[])
{
char buff[] = "helloworld";
//fun(buff);//error: invalid initialization of non-const reference of type 'char*&' from a temporary of type 'char*'
char *p = buff;
fun(p);//运行正确
return 0;
}