数组名是首元素的地址 唯独这两种情况
1、sizeof(数组名) 此时表示整个数组的长度 2、&数组名 此时表示整个数组的地址
整形数组
#include <stdio.h>
int main()
{
int arr[]={1,2,3,4};
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[1]));
printf("%d\n",sizeof(&arr));
printf("%d\n",sizeof(*&arr));
printf("%d\n",sizeof(&arr+1));
printf("%d\n",sizeof(&arr[0]));
printf("%d\n",sizeof(&arr[0]+1));
return 0;
}
字符数组
#include <stdio.h>
int main()
{
char arr[] = { 'a','b','c','d','e','f' };
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));
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;
}
#include <stdio.h>
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));
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;
}
指针变量
#include <stdio.h>
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));
printf("%d\n", strlen(p));
printf("%d\n", strlen(p+1));
printf("%d\n", strlen(*p));
printf("%d\n", strlen(p[0]));
printf("%d\n", strlen(&p));
printf("%d\n", strlen(&p+1));
printf("%d\n", strlen(&p[0]+1));
return 0;
}
二维数组
#include <stdio.h>
int main()
{
int arr[3][4] = { 0 };
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr[0][0]));
printf("%d\n", sizeof(arr[0]));
printf("%d\n", sizeof(arr[0]+1));
printf("%d\n", sizeof(*(arr[0]+1)));
printf("%d\n", sizeof(arr+1));
printf("%d\n", sizeof(*(arr+1)));
printf("%d\n", sizeof(&arr[0]+1));
printf("%d\n", sizeof(*(&arr[0]+1)));
printf("%d\n", sizeof(*arr));
printf("%d\n", sizeof(arr[3]));
return 0;
}
指针的大小是 4/8 地址的大小是 4/8 都取决于32位的电脑还是64位的电脑 详情 指针的大小
|