Array names can be used as constant pointers, so can pointers be used as array names?
Subscript vs Pointer Form:
Sample program:
1#include <stdio.h>2 3 intMain ()4 {5 inta[5] = {0};6 int* p =A;7 inti =0;8 9 for(i=0; i<5; i++)Ten { OneP[i] = i +1; A } - - for(i=0; i<5; i++) the { -printf"a[%d] =%d\n", I, * (A +i)); - } - +printf"\ n"); - + for(i=0; i<5; i++) A { atI[a] = i +Ten; - } - - for(i=0; i<5; i++) - { -printf"p[%d] =%d\n", I, P[i]); in } - to return 0; +}
The 11th line uses the pointer as an array name.
The results of the operation are as follows:
You can see that the output is correct.
But arrays and pointers are different.
Sample program:
1#include <stdio.h>2 3 intMain ()4 {5 extern int*A;6 7printf"&a =%p\n", &a);8printf"A =%p\n", a);9printf"*a =%d\n", *a);Ten One A return 0; -}
EXT.C file
1 int a[] = {12345};
When the above procedure 5th Act extern int a[]; The result of the compilation run is as follows:
When line 5th is changed to extern int *a, the result is as follows:
A segment error has occurred.
When compiling ext.c, the compiler allocates 20 bytes in memory when the following happens in memory:
The compiler compiles ext.c, allocates memory, and gives an address of 0x804a014. The compiler maps the identifier of a to the address value of 0x804a014. There are no variables and identifiers in memory, only addresses.
When the compiler compiles to the 5th line of the main program and sees that a is defined externally, it is assumed that the compiler has given it an address value, and that the 7th row takes an address value of a, which naturally gets 0x804a014.
When compiling to the 8th line, A is interpreted as a pointer, and a variable is assigned an address of 0x804a014 in the compiler, which is actually invisible to the programmer. The value of a is not the address, but the value stored in the address, so the compiler takes four bytes to the address of the 0x804a014, which is the value of a of four bytes.
29th lesson pointer and array analysis (bottom)