How the compiler works: on a 64-bit computer, when you create a pointer variable, the computer allocates 8 bytes of storage space for it. But what if you are creating an array? The computer allocates storage for an array, but does not allocate any space to the array variable, and the compiler replaces it with the starting address of the arrays only where it appears.
Conclusion 1: Because the computer does not allocate space for an array variable, it cannot be pointed to elsewhere. Examples:
Char s[]="How arebig is it? " " ; Char *t=s; // correct, assign the address of the array to the pointer variable ts=t; // error, array variable has no storage space, cannot store value of pointer variable t, compile error
Conclusion 2: If an array variable is & with the address character, the result is the variable itself.
#include <stdio.h>intMainintargcChar*argv[]) { intarr[3]={1,2,3}; printf ("arr=%p\n", arr); printf ("&arr=%p\n",&arr); return 0;} The result is: [email protected]:~$ ./Tarr=0x7ffed97ce8d0&arr=0x7ffed97ce8d0
Conclusion the value of 3:sizeof (array) is the size of the array, and sizeof (pointer) is the size of the previous address of the operating system, 8 bytes on the 64-bit machine, and 4 bytes on the 32-bit machine.
#include <stdio.h>intMainintargcChar*argv[]) { intarr[3]={1,2,3}; int*p=arr; printf ("sizeof (arr) =%d\n",(int)sizeof(arr)); printf ("sizeof (p) =%d\n",(int)sizeof(p)); return 0;} Output: [email protected]:~$ ./Tsizeof(arr) = Asizeof(p) =8
Conclusion 3 shows that if the array variable is assigned to a pointer, then the pointer variable will only contain the address information of the array, and the length of the arrays is unknown, which is equivalent to the loss of some information by the pointer. We call this loss of information a degenerate. As long as the array variable is passed to the function, the array is inevitably degraded to a pointer, so passing the array to the function requires a clear indication of the size of the array. To this:
int arr[3]={1,2,3}; int sum_arr (int arr[],int n); // explicitly indicate array size with integer variable n
The difference between array name and pointer variable in C language