sizeof的实例
例题一:求arr数组的大小,单位为字节。
int main() {
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr + 0));
printf("%d\n", sizeof(*arr));
printf("%d\n", sizeof(arr[1]));
printf("%d\n", sizeof(&arr));
printf("%d\n", sizeof(&arr + 1));
printf("%d\n", sizeof(&arr[0] + 1));
return 0;
}
strlen实例
计算长度,不能是整型,只能是地址!!!!
```
int main() {
char arr[] = "abcdef";
printf("%d\n", strlen(arr));
printf("%d\n", strlen(arr + 0));
printf("%d\n", strlen(*arr));
printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr + 1));
printf("%d\n", strlen(&arr[0] + 1));
return 0;
}
![](http://www.icode9.com/i/li/?n=4&i=images/blog/202104/12/0dd076a8d5d4b0ae848e279bf8f11c41.png?,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
![](http://www.icode9.com/i/li/?n=4&i=images/blog/202104/12/7c319b7b31e6d8349593f350c3fa649e.png?,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
## 指针数组中sizeof的应用
注意,char *p="abcdef",只是把a的地址存放到p中,因为p是char *类型,存放不下“abcdef".但是我们的p通过a就能找到abcdef\0.
int main() {
char p = "abcdef";
printf("%d\n", sizeof(p));
printf("%d\n", sizeof(p+1));
printf("%d\n", sizeof(p));
printf("%d\n", sizeof(p[0]));
printf("%d\n", sizeof(&p));
printf("%d\n", sizeof(&p+1));
printf("%d\n", sizeof(&p[0]+1));
return 0;
}
![](http://www.icode9.com/i/li/?n=4&i=images/blog/202104/12/b158340bae26ff174e4722da5797031f.png?,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
![](http://www.icode9.com/i/li/?n=4&i=images/blog/202104/12/782bcb657e1d1a7b36b90ff3da8561b8.png?,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
## 二维数组中sizeof和strlen的应用
int main() {
int a[3][4] = { 0 };
printf("%d\n", sizeof(a));
printf("%d\n",sizeof(a[0][0]));
printf("%d\n", sizeof(a[0]));
printf("%d\n", sizeof(a[0]+1));
printf("%d\n", sizeof((a[0]+1)));
printf("%d\n", sizeof(a+1));
printf("%d\n", sizeof((a+1)));
printf("%d\n", sizeof(&a[0]+1));
printf("%d\n", sizeof((&a[0]+1)));
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a[3]));
}
![](http://www.icode9.com/i/li/?n=4&i=images/blog/202104/12/cffcfed14720a4aa1bd78980ebea3f7f.png?,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
![](http://www.icode9.com/i/li/?n=4&i=images/blog/202104/12/feaa99248bb80b0f39656c6e85cc4c28.png?,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)