int a[3];中a+1与&a+1差别 -- C


int a[3];

a 和 &a 的地址一样的。

a+1 == a + 1*sizeof(int);跳跃是一个数组元素大小

&a+1 == a + 3*sizeof(int);跳跃是整个数组大小


#include <stdio.h>

int
main()
{
	char * a[] = {"hello","the","world"};
	char ** pa = a;
	pa ++;
	
	/*	获取数组中第二个元素 */
	printf("*pa = %s
",*pa);
	printf("*(a+1) = %s
",*(a+1));

	int b[] = {1,2,3};
	int * pb = b;

	/*	获取数组中最后一个元素 */
	printf("*((int *)(&b +1)-1) = %d
",*((int *)(&b +1)-1));
	printf("*((int *)((&b +1)-1) = %d
",*((int *)((&b +1)-1)));

	printf("*((char **)(&a+1)-1) = %s
",*((char **)(&a+1)-1));

	/*	地址一样 */
	printf("b = 0x%0X
",b);
	printf("&b = 0x%0X
",&b);
}
/*
[root@localhost test_class]# ./a.out 
*pa = the
*(a+1) = the
*((int *)(&b +1)-1) = 3
*((int *)((&b +1)-1) = 1
*((char **)(&a+1)-1) = world
b = 0xBFC556B0
&b = 0xBFC556B0
*/


原文地址:https://www.cnblogs.com/gcczhongduan/p/4005325.html