Preface
As for pointers, we first need to figure out four points: the pointer type, the pointer to the type, the value of the pointer, and the value the pointer points to. If this is not very clear, please refer to here
URL: http://blog.csdn.net/hsd2012/article/details/50946407
When the const modifier is a pointer type, the value of the pointer cannot be changed, that is, it cannot point to another address, but it can be modified by the pointer to the value in the address.
When the const modifier is the type that the pointer points to, the pointer is pointed to another address, but the value in the pointer cannot be modified by the pointer.
1) Start with the const int i. I we call symbolic constants by using the const modifier. That is, I cannot be assigned to another place. NOTE: the const int I and the int const I are equivalent, the same, that is, the position of the const and int does not matter.
2) const int *PSee Example:
int i1=30;
int i2=40;
const INT *p=&i1;
p=&i2; Here, p can reassign a new memory address at any time.
i2=80; Can we replace it with *p=80? The answer is no.
printf ("%d", *p); Output 80
Analysis: The value of P can be modified, that is, it can be pointed back to another address. However, you cannot modify the I2 value by *p.
First, the const modifier is the entire *p (note that it is *p instead of P), so *p is a constant and cannot be assigned a value. Although P refers to i2 as variables rather than constants;
Second, p is not modified by a const, so p is a pointer variable. Can be assigned to point back to another memory address.
3 int * Const Pint i1=30;
int i2=40;
int * Const p=&i1;
p=&i2; Wrong. P can no longer point to another new address.
I1=80//Here can be replaced with *p=80, that is, you can modify the I1 value by *p.
printf ("%d", *p);
Analysis:
At this point, the value of P can not be assigned to modify, can always point to the initialization of the memory address. Instead, you can modify the value of I1 with *p.
So: To sum up,
Pointer p is a pointer constant because it has a const modifier, that is, the pointer p cannot be modified.
The entire *p is not preceded by a const modifier, then the *p is a variable rather than a constant, so the *p value can be changed.
If the const modifier is *p before it is *p, it does not mean p;
If the const is written directly before p, then p cannot be changed.
4) Supplementaryconst int I1=40;
int *p;
p=&i1; Hint error, unable to convert from const int to int.
----------------------------------------
const int I1=40;
const int *p;
p=&i1; Two types are the same, you can assign this value.5 about pointers and array problemsIn programming, you will encounter a situation like this:
int *p[n];
int (*p) [n];
There is also a case for declaring a function:
int *p ();
Int (*p) ();
What do these distinctions mean?
On the surface these problems seem to be pointers, but how to correctly judge these definitions requires that we first have the concept of operator precedence.
In C, [] and () have higher precedence than asterisks (the name of the asterisk is called the "pointer Operator"), so in these declarations, look at [] and () first.
Also, the binding order of the operators of the two parentheses is left to right. Thus, int *p[n] is actually quite the same as int * (p[n]), i.e. (int *) (p[n)).
Let's step through the analysis:
(int *) (P[n]) first is an array (for example, to replace int with int, which is "an array of elements that are shaping data.") So, for int*, the element is an array of pointers to the shaping data.
And look at another:
int (*p) [n] Changes the precedence of the natural operator, equivalent to (int) ((*p) [n]). A pointer on the headache, we replace the pointer first, that is, int a[n], is an n-dimensional array, the first address of the array (that is, the array name) is a.
So, int (*p) [n] is also an n-dimensional array, but the first address of the array is *p, that is, the content that P points to is the first address of an array.
So, p is a pointer to an array, and the elements in that array are int. In fact, p is equivalent to a two-dimensional pointer.
Similarly, you can analyze int *p () and int (*p) ();
Notice that here we have different meanings for the asterisk. elaborated as follows:
int A;
int *p=&a;//defines pointer p to a
*p = 10;
Here, the asterisk in the second line says, "defines a pointer to an int type of data. Because in fact, * is equivalent to (int *), defines a pointer to int type data.
The asterisk in the third row represents the data that P points to, because there is nothing in front of the asterisk that represents a type such as int void char.
In other words, if the asterisk has previous data such as the int,void of the type, we think that the meaning of the asterisk here is "define a pointer"
If there is nothing in front of the asterisk that represents the type, then we think that the asterisk here means "the data that the pointer points to".
So, let's look back at int *p[n] and int (*p) [n].
Since we have just analyzed the priority, the former corresponds to (int *) (P[n]), which is equivalent to int ((*P) [n])
Based on our previous conclusion about the asterisk, the former indicates that an array of length n is defined (P[n]), and each element is a pointer to the int data (int *).
The latter represents the definition of an int a[n]. Everyone here understands that, and then replace a with *p, which means that p is a pointer, and the content it points to (that is, *p) is the first address of an array (a)