当引用一个数组作为形参时:
#include <iostream>
void a(unsigned char (&tmp)[200])
{
printf("%x\r\n",tmp);
sprintf((char *)&tmp,"aaaaaa");
}
int main(int argc, char **argv)
{
unsigned char tmp[200] = {0};
printf("%x\r\n",tmp);
a(tmp);
printf("%s",tmp);
return 0;
}
输出结果:
61fd40
61fd40
aaaaaa
形参不想指定数组大小该如何编写?
#include <iostream>
template<unsigned N>
void a(unsigned char (&tmp)[N])
{
printf("%x\r\n",tmp);
sprintf((char *)&tmp,"aaaaaa");
}
int main(int argc, char **argv)
{
unsigned char tmp[200] = {0};
printf("%x\r\n",tmp);
a(tmp);
printf("%s",tmp);
return 0;
}
输出结果:
61fd40
61fd40
aaaaaa
详细内容可以搜索:非类型模板参数
|