Pointer advanced-ONE (including a deeper understanding of arrays)

This is a series of questions.
The problem of pointers is more tormenting for beginners, especially inseparable from arrays. Let's take a look at a problem.

#include <stdio.h>
int main(void)
{
    
    
    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));
    printf("%d\n", sizeof(*a));
    printf("%d\n",sizeof(&a+1));
    return 0;
}

Calculate the output of these few.
Let's take a look at sizeof() first, this keyword.

sizeof() is the size of the calculation type. In bytes.
Such as: sizeof(int)------4

And the above problem is to calculate the size of the type in the brackets.
Let’s look at the conditions like two rules first

sizeof (array name)-array name at this time represents the entire array
& array name-represents the entire array.
In addition, all represent the address of the first element of the array.
Moreover, in the memory, no matter what type of element address, the address size is It occupies 4 bytes in size.

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

At this time, a, represents the entire array, calculate the size of the memory occupied by the entire array, int occupies 4 bytes,
then the answer is 4*4=16 bytes.

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

At this time, a+0 is not a single array name, and does not mean the entire array. It represents the address of the first element, and the address only occupies 4 bytes (don’t know if you look up). The answer is 4.

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

Take the address character and meet two conditions. It means to take out the address of the array, but the address of the array is also the first element, which is the address, which only occupies 4 bytes.

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

The addresses are taken and dereferenced, which cancels each other out. And it is equivalent to a single a, same as above, 16 bytes.

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

This &a+1 is not the same as a+1, a+1 and the second question are a type, and &a gets the address of the entire array. Although it is the same as the first element, &a is a type that occupies 4 ints The address of the object, and a is the address of an object of type int. The object represented is different, the size of the pointer movement is different,
Insert picture description here
but &a+1 still represents the address, which is 4 bytes.
Also, to be released.
If you have any questions, please let me know.
Advanced pointer-TWO

Guess you like

Origin blog.csdn.net/weixin_52199109/article/details/112297949