一,用sizeof运算符可以算出数组的容量,而把这个运算符应用于指针,得到的是指针变量的大小
int main(){
char *p = "hello";
char str[10] = "hello";
cout<<sizeof(p)<<endl;
cout<<sizeof(str)<<endl;
}
输出结果
4
10
Process returned 0 (0x0) execution time : 0.006 s
Press any key to continue.
二,函数中数组名作为形参时,在函数体内数组名失去本身的内涵,仅仅只是一个指针
1,函数名作为函数形参
void Func(char str[], int size){
cout<<sizeof(str);
}
int main(){
int size = 20;
char str[size];
Func(str, size);
return 0;
}
输出结果
4
press any key to continue.
2,数组名不作为函数形参
int main(){
char str[20];
cout<<sizeof(str);
return 0;
}
输出结果
20
press any key to continue.
3,总结
a,数组名指代一种数据结构,这种数据结构就是数组,此时str指代数据结构char[20]。
char str[20];
cout<<sizeof(str);
//输出结果为:20
b,数组名是一个指针常量,不能做自加、自减、赋值等操作。
c,数组名作为函数形参时,沦为一个普通的指针。