小端:
//一维数组
int a[] = { 1, 2, 3, 4 };
printf("%d\n", sizeof(a)); //16
printf("%d\n", sizeof(a + 0)); //4
printf("%d\n", sizeof(*a)); //4
printf("%d\n", sizeof(a + 1)); //4
printf("%d\n", sizeof(a[1])); //4
printf("%d\n", sizeof(&a)); //4
printf("%d\n", sizeof(*&a)); //16
printf("%d\n", sizeof(&a + 1)); //4
printf("%d\n", sizeof(&a[0])); //4
printf("%d\n", sizeof(&a[0] + 1)); //4
//字符数组
char arr[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
printf("%d\n", sizeof(arr)); //6
printf("%d\n", sizeof(arr + 0)); //4
printf("%d\n", sizeof(*arr)); //1
printf("%d\n", sizeof(arr[1])); //1
printf("%d\n", sizeof(&arr)); //4
printf("%d\n", sizeof(&arr + 1));//4
printf("%d\n", sizeof(&arr[0] + 1));//4
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));//&a是数组指针,不是字符指针,不能求 char(*p)[6];
//printf("%d\n", strlen(&arr + 1));//同上
printf("%d\n", strlen(&arr[0] + 1));//?
//因为&a取的是整个数组的地址,而strlen只能用来求字符串,由于C语言中没有字符串类型,字符串只能存储在字符数组中或由字符指针指向,所以strlen()中的必须为字符指针。
char arr[] = "abcdef";
printf("%d\n", sizeof(arr)); //7
printf("%d\n", sizeof(arr + 0));//4 arr+0视为地址
printf("%d\n", sizeof(*arr));//1
printf("%d\n", sizeof(arr[1]));//1
printf("%d\n", sizeof(&arr));//4
printf("%d\n", sizeof(&arr + 1)); //4
printf("%d\n", sizeof(&arr[0] + 1));//4
printf("%d\n", strlen(arr)); //6
printf("%d\n", strlen(arr + 0)); //6
//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));//5
char *p = "abcdef";
printf("%d\n", sizeof(p)); //4
printf("%d\n", sizeof(p + 1)); //4
printf("%d\n", sizeof(*p)); //1
printf("%d\n", sizeof(p[0])); //1
printf("%d\n", sizeof(&p)); //4
printf("%d\n", sizeof(&p + 1)); //4
printf("%d\n", sizeof(&p[0] + 1)); //4
printf("%d\n", strlen(p)); //6
printf("%d\n", strlen(p + 1)); //5
//printf("%d\n", strlen(*p));//*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)); //5
//二维数组
int a[3][4] = { 0 };
printf("%d\n", sizeof(a)); //48
printf("%d\n", sizeof(a[0][0])); //4
printf("%d\n", sizeof(a[0])); //16
printf("%d\n", sizeof(a[0] + 1)); //4
printf("%d\n", sizeof(*(a[0] + 1)));//4 a[0][1]
printf("%d\n", sizeof(a + 1));//4
printf("%d\n", sizeof(*(a + 1)));//16
printf("%d\n", sizeof(&a[0] + 1));//4
printf("%d\n", sizeof(*(&a[0] + 1)));//16
//&a[0]+1等价于a+1
printf("%d\n", sizeof(*a));//16
printf("%d\n", sizeof(a[3]));//16
//cpp[-1][-1]=*(*(cpp-1)-1);
int main()
{
int a[3][4];
printf("%p\n", a);
printf("%p\n", a+1);
printf("%p\n", &a+1);
printf("%p\n", a[0]+1);
printf("%p\n", &a[0]+1);
system("pause");
return 0;
}
sizeof()中,a[0]+1为地址,a[0]+1等价于a [0] [1],a[0]相当于a[3] [4]第一行的数组名,即a[0]相当于一个含四个元素的一维数组,故a[0]+1等价于a[0] [1]
a为数组
a+0为地址
int a[]={1,2,3,4,5,6,7,8,9,10};
int *p1=&a[0];
int *p2=&a[10];
int n=p2-p1=10;
//指针相减的值为相差数据的个数
以上就是本篇文章的内容,介绍了大小端的概念和指针与地址的一些题目,题目包含了初学指针常见的一些错误,仔细研究定能有所收获。