Let's look at a common example:
#include <stdio.h>
int main (void)
{
int a[3] = {n/a};
int *p = A;
printf ("%d", p[0]);
return 0;
}
This code compiles and runs without any problems, the program will print out the value of 1, but why can you use it? P is clearly an int type pointer, how can we use p[0] this array operation? And we use sizeof to test A and p to get one is the size of a array, one is the size of the P pointer, these two types are not the same. In fact, this is the C language internal reason, a pointer variable in the use of similar p[] such an operation, the compiler is equivalent to P did a type promotion, the p is promoted to the array of that type, note that this will only ascend once Oh! Look at the following example:
#include <stdio.h>
int main (void)
{
int a[3][3] = {1,2,3,4,5,6,7,8,9};
int *p = (int *) A;
printf ("%d", p[0]);
return 0;
}
The first thing to note is that (int *) This is necessary because the first address of an int type two-dimensional array cannot be directly assigned to an int * variable. Next this code also can output 1 normally, the understanding of this program is similar to the previous one. Take a look at the following code:
#include <stdio.h>
int main (void)
{
int a[3][3] = {1,2,3,4,5,6,7,8,9};
int *p = (int *) A;
printf ("%d", p[0][0]);
return 0;
}
Will this code compile or run with errors? The answer is error when compiling, because P is a pointer of int type, when using [] This operation, the compiler will raise it once, only once, so P will be promoted to the degree of one-dimensional array, but the operation of this p[0][0 is obviously for a two-dimensional array, The compiler does not allow this usage. Let's look at the following example:
#include <stdio.h>
int main (void)
{
int a[3][3] = {1,2,3,4,5,6,7,8,9};
int (*p) [3] = A;
printf ("%d", p[0][0]);
return 0;
}
Note that this example, compile and run without error, because p is an array pointer, a directly assigned to P also does not error, here also shows that the array in the pointer to this type of assignment, the compiler defaults to a maximum of one level, that is, one-dimensional array to the pointer directly to the compiler allows the value, A two-dimensional array gives this type of array pointer assignment to the compiler, but it doesn't go across level two, like the previous example. As can be seen from here, p[0][0] Such operations are allowed, p is promoted to a two-dimensional array, p is also promoted to a level. This code will print out 1. Let's look at the following example:
#include <stdio.h>
int main (void)
{
int a[3][3] = {1,2,3,4,5,6,7,8,9};
int (*p) [3] = A;
printf ("%d", p[1]-p[0]);
return 0;
}
This code compiles and runs without error, what is the printed value? First to analyze the program, P[1] p represents an array pointer, but using p[] This operation, p will be promoted to a two-dimensional array form, then p[1] is equivalent to a[1],p[0] is equivalent to a[0], then a[1]-a[0] The value of what? A[1] is the first address of the second [3] one-dimensional array in a two-dimensional array A, and a[0] is the first address of a one-dimensional array in a, so the two-first address is a difference between sizeof (int), but since this is the pointer (address) operation, the value here should be (sizeof ( int)/sizeof (int), so the resulting output is 3. Then look at the following code:
#include <stdio.h>
int main (void)
{
int a[3][3] = {1,2,3,4,5,6,7,8,9};
int (*p) [3] = A;
printf ("%d", *p[0]);
return 0;
}
What does this piece of code output? The answer is output 1. The first thing you need to know is [] the priority of the operation is higher than the * operator, then p[0] represents the first address of a a[0] that is, and then uses the * (address) of this notation, take the value of this address, so that is the value of a[0][0], because A[0]=&a[0] [0]. Let's look at the following example:
#include <stdio.h>
int main (void)
{
int a[3][3] = {1,2,3,4,5,6,7,8,9};
int (*p) [3] = A;
printf ("%d", * (P+1) [0]);
return 0;
}
What does this piece of code output? The answer is 4. First P is an array pointer, so the p+1 operation corresponds to the past compiler understanding refers to the next one-dimensional array of the first address, so p+1 actually value is p+1*sizeof (int), the following understanding is the same as above.
C-language pointers--pointers and arrays