C language pointer should be learned like this?

The meaning of the array name:


1. sizeof(array name), where the array name represents the entire array, and the calculation is the size of the entire array .
2. &Array name, where the array name represents the entire array, and the address of the entire array is retrieved.
3. All other array names represent the address of the first element.

Solve the following questions based on the meaning of the above array names:

#include<stdio.h>

int main()
{
	int a[] = { 1,2,3,4 };
	printf("%d\n", sizeof(a));
	printf("%d\n", sizeof(a + 0));
	printf("%d\n", sizeof(*a));
	printf("%d\n", sizeof(a + 1));
	printf("%d\n", sizeof(a[1]));
	printf("%d\n", sizeof(&a));
	printf("%d\n", sizeof(*&a));
	printf("%d\n", sizeof(&a + 1));
	printf("%d\n", sizeof(&a[0]));
	printf("%d\n", sizeof(&a[0] + 1));
}

What are all the results printed out, let's solve these problems one by one?

1:

printf("%d\n", sizeof(a));

 printf("%d\n", sizeof(a)); //sizeof(a) calculates the size of the entire array, 4 elements, each element is 4 bytes, so the final size is: 16 byte.

2:

printf("%d\n", sizeof(a + 0));

 3:

printf("%d\n", sizeof(*a));

 

4:printf("%d\n", sizeof(*a));

 5:printf("%d\n", sizeof(a + 1));

 

 6:printf("%d\n", sizeof(a[1]));

 7:printf("%d\n", sizeof(&a));

 8:printf("%d\n", sizeof(*&a));

9: printf("%d\n", sizeof(&a + 1));

10: printf("%d\n", sizeof(&a[0]));

 11:printf("%d\n", sizeof(&a[0] + 1));

 

Guess you like

Origin blog.csdn.net/xingyuncao520025/article/details/132005058
Recommended