This edition discusses many of the ideas and perceptions of pointers and arrays in C language, Below is my own some superficial understanding of the pointer, write a bit messy, I hope to be helpful to the novice Please criticize us. We also welcome the active discussion.
1. Pointer variables are just normal variables
Many beginners in C are very secretive about pointer variables, in fact, like other common variables (such as int), pointer variables are a common variable, and he has all the characteristics of other variables.
For example: int main () { int q=10; int *pi=0; PI = &q; printf ("%d\n", *PI); }
An automatic variable p is declared and defined in main, and his type is pointer-to-int. Once the P is defined, the compiler will allocate the memory space to P. When main ends, p is automatically released. PI and q are not the slightest difference in these areas.
Conclusion: pointer variable is not so mysterious, pointer variable is just a normal variable
2. The relationship of pointer variables to other variables
So, where does the specificity of pointers appear?
The pointer is special in the interpretation of the value he stores and on his use.
In the above example, the value of pi is interpreted as an address in memory, and a chunk of memory starting at that address represents the number of an int.
However, even after pi = &q, pi and Q have no direct relationship: Changing the value of pi does not affect Q, and changing the value of Q does not affect PI, they are two independent variables and have their own storage space.
The C language gives pointers a special ability to store the memory addresses of other variables, and to manipulate the variables indirectly using the value of the pointer variable itself. These two abilities are done through two operators: & and *.
PI = &q; Use the memory address of Q to assign a value to pi printf ("%d\n", *PI); Use the value of pi to read the value of Q indirectly
sizeof (PI) and sizeof (q) are fundamentally different. PI will not be automatically based on their own to find Q, only you show the use of *PI.
Conclusion: It is assumed that the pointer Pi stores the address of Q. PI and Q do not have any direct relationship. Only *pi and Q have a direct relationship.
3. How to transfer function parameters in C language
Another misconception that many people have is that there are two ways to pass a function in C: pass by value and pass by address.
The reason for this misunderstanding is that there is not enough understanding of the two points mentioned above.
void Swap1 (int a, int b) { int temp; temp = A; A = b; b = temp; }
void Swap2 (int* pa, int* pb) { int temp; temp = *PA; *pa = *PB; *PB = temp; }
int main () { int i=10, j=5; int *pi, *PJ; PI = &i; PJ = &j; Swap1 (i, j); printf ("i=%d, j=%d\n", I, j); Swap2 (pi, PJ); printf ("i=%d, j=%d\n", I, j); }
Many people think that SWAP1 is passed by value, while SWAP2 is passed by pointer. In fact, C language has only one function parameter passing way: passing by value.
Swap1 We all understand, I say swap2.
In fact, I have pointed out in the 1th that the pointer is just a normal variable. In the call to Swap2 swap2 (pi, PJ), the values of pi and PJ are copied to the PA and PB in Swap2, respectively. Now, I,PI and PA are 3 completely different variables, but the values of pi and PA are the same, they all have the address of I. In the SWAP2, the use of *PA can read and write I, the same use of *PB can read and write J, so SWAP2 can complete I and J Exchange. However, the values of pi and PJ cannot be changed by SWAP2.
That is, the argument passed by SWAP2 is still passed by value, except that the value of the pointer is passed, and then the pointer has the ability to indirectly access other variables.
Conclusion: C language has only one function parameter passing mode: passing by value.
4. Pointers and Arrays
4.1 Types of array names
In C language, pointers are inextricably linked to arrays. See the following example:
int a[5]; int b[3][5]; int *pa1[5]; int (*PA2) [5];
So what is the type of A,B,PA1,PA2?
Many people misunderstand the type of a as a first-level pointer, the const pointer to int,
The type of B is misunderstood as a level two pointer, that is, the const pointer to the pointer to int;
A is not a const pointer to int type, we can launch it from the following fact: sizeof (a) is different from sizeof (int*).
Can only say that a is an array of 5 ints type
and b is the array of 3 arrays of 5 ints type
Here the reason to put PA1 and pa2 out, mainly for everyone to distinguish: PA1 is a type of array of 5 int pointers, And PA2 is pointer to array of 5 ints type
4.2 Operation of array names
You often encounter arithmetic problems with array names, such as
int A[5]={1, 2, 3, 4, 5}; int b[3][5]={{1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15}}; printf ("%d", * (a+2)); printf ("%d,%d\n", * * (B+2), * (*b+2));
In doing the above operation, there is the following seemingly non-conclusion:
&a can be seen as a pointer to array of 5 ints types So &a+1, the "1" here refers to 5*sizeof (int)
A is an array of 5 ints type, but what is the value of a? The value of a is actually a Pointer to int
The value of *a is an int, or a[0];
&b can be seen as pointer to array of 3 arrays of 5 ints types So &b+1, the "1" here refers to 3*5*sizeof (int)
b is an array of 3 arrays of 5 ints type, but the value of B is a Pointer to array of 5 ints
*b is an array of type 5 ints, but the value of *B is pointer to int
The value of **b is of type int, that is, b[0][0];
By and by, for int c[n1][n2] ... [nm] m-dimensional array (the number behind M is subscript), with the following conclusion (or calculation method): First expand C to int c[n1][n2] ... [NM] [1]; (The last one is the number 1, not the letter L)
1.&c unit "1" is n1*n2*...*nm*1*sizeof (int) sizeof (&C) is equal to the size of a pointer stored with the same value as sizeof (int*);
2. Make C1 representative *...*c, a total of I (0<i<=m-1) *, then his unit "1" is n (i+2) *...*nm*1*sizeof (int), where (i+2) and so is subscript sizeof (C1) =n (i+1) *...*nm*sizeof (int)
3. Make C2 represent *...*c, a total of M *, representing the first integer in the array sizeof (C2) =sizeof (int)
Example: int c[3][4][5][6]; First converted to int c[3][4][5][6][1]; &c+1 is equivalent to address plus 3*4*5*6*1*sizeof (int), and sizeof (&c) equals sizeof (int*); C+1 equivalent to address plus 4*5*6*1*sizeof (int), sizeof (c) =3*4*5*6*1*sizeof (int); **c+1 equivalent to address plus 6*sizeof (int), sizeof (**C) =5*6*1*sizeof (int); C is the first integer in the array
4.3 A common error
int b[3][5]; int **p=b;
Some people mistakenly think that the value of p+1 is the same as b+1.
Similar to
T1 B1; t2* b2= (t2*) (&B1); T1,t2 for two different types
In this case, b2+1 is increased by sizeof (T2). And &b1+1 is increased according to sizeof (T1). When a pointer is converted to a type, its operation is related to his own type, not to what he points to. |