C language is not simple: array and pointer, array pointer
When I was writing C, I didn't pay much attention to the array. So I defined one and used it upside down. But then I encountered a problem. After the solution, I decided to write such a blog. The array cannot be separated from the pointer, So I simply put it together.
Now I have defined an array: int cc [10];
There are several pointers around this array: cc, cc + 1, & cc [0],& Cc, & cc + 1And so on. Do you know what they mean? Run the following code:
#include <stdio.h>int main(){ int cc[10]; printf("%x\n", cc); printf("%x\n", cc+1); printf("%x\n", &cc[0]); printf("%x\n", &cc); printf("%x\n", &cc+1); getchar(); return 0;}
Cc, which is the first "Pointer" to be exposed when learning arrays. It is the first element of arrays.
Cc + 1, which is a pointer to the second position of the number.
& Cc [0], which is actually cc, pointing to the first element of the array.
& Cc, what is this? Pointer to pointer?
& Cc + 1. If the above is a pointer to a pointer, isn't it a pointer to a wild address?
Assume that the running environment is a 32-bit server and the first address of the array is 0x28ff00:
There is no doubt that the cc result is 0x28ff00.
The cc + 1 address is 0x28ff04 rather than 0x28ff01, because an int occupies 4 bytes of space. Cc + 1 is actually treated as cc + 1 * sizeof (int.
The result of & cc [0] Is 0x28ff00, and cc [0] indicates the first element of the array. Then & cc [0] is the address of the first element. & Cc [0] = cc.
& Cc, this is hard to say. The value of the pointer cc is 0x28ff00, and & cc indicates the address of the pointer. How can we know this address? Is the output random address? The random number is meaningless. Isn't it a random address or 0x28ff00? In this case, a is equal to &? Obviously not...
For pointers of basic types, such as int * tt; * tt is its value, & tt is the pointer address, & tt! = Tt
However, the above cc is an array. In fact, & cc is compiled into & cc [0], but it has different meanings. & cc points to the beginning of the entire array. & Cc and cc points can be used for image representation:
As you can see, & cc actually represents int (*) [10], so & cc + 1 can be understood as cc + sizeof (cc)/4, the reason for dividing the value by 4 is that the int pointer ++ actually moves four bytes.
Or % cc = cc + sizeof (cc)/4 = cc + 10, so the value of & cc + 1 is 0x28ff28.
We can see that the array name we usually use is not simply treated as a pointer. An array name represents the variable name of an array object. It is a left value and a left value that cannot be changed. However, because the size of the array is not saved in the program, only the left value of the array can be accessed through the array name, and the right value of the array cannot be accessed. For this reason, when the array name is used as the right value, it is given another new meaning-the pointer to the first element of the array, which is the array-to-pointer conversion rule. According to the standard, only when the array name is used as the operand of sizeof and & operator, it is a left value and its type is array. In all other cases, the array name is a right value and is automatically converted to the pointer type by the compiler. In this case, the array name is a pointer and a pointer constant.
Next is something interesting. We can combine sizeof with the array to output various values. What are the output results of the following programs? We recommend that you think about it and then run the program to verify the answer.
#include <stdio.h>int main(){ int cc[10]; printf("%d\n", sizeof(cc[0])); printf("%d\n", sizeof(cc)); printf("%d\n", sizeof(&cc)); printf("%d\n", sizeof(int(*)[10])); getchar(); return 0;}
Sizeof (cc [0]), the size of an int, output 4, no problem.
Sizeof (cc). Do not mix it with the above. This is not the pointer to the first address of the array. cc here is the left value, which is an array type, so the result is 40.
Sizeof (& cc), what is the answer? Note that cc is still the left value here. It is an array type, but & cc is different from cc. No matter how complicated the array is, it is always a pointer, the pointer size on a 32-bit machine is always 4 bytes, so the result is 4.
Sizeof (int (*) [10]), which is similar to the above & cc, representing the entire array, but still a pointer, so the result is also 4.
Exercise: What is the output result of the following Program (assuming a 32-bit server ):
#include <stdio.h>int main(){ int *p[2]; printf("%d\n", *p); printf("%d\n", sizeof(p)); printf("%d\n", sizeof(&p)); getchar(); return 0;}
The answer is: Random Number, 8, 4
Another exercise: What is the output of the following programs?
#include <stdio.h>int main(){ char str[]="hactrox"; char *p = str; printf("%d %d\n", sizeof(str), sizeof(p)); getchar(); return 0;}
Answer: 8, 4 (do not forget '\ 0 ')
C language array and pointer, very simple !!!
Simply put,
Int * p [10]; is a two-dimensional array that determines the number of rows (10 rows), each row has several elements, it can be dynamically specified in the Program (just as the following program uses p [1] = malloc (sizeof (int) * 15 ); specify the p [1] line as 15 elements); you can dynamically create an irregular array.
Int (* s) [10]; is a two-dimensional array that determines the number of elements in each row (10), specific rows, it can be dynamically specified in the Program (like the following program using s = malloc (sizeof (int *) * 10); the number of lines of s can be specified as 10 );
# Include <stdio. h>
# Include <stdlib. h>
Int main (int argc, char * argv [])
{Int * p [10];
Int (* s) [10];
P [1] = malloc (sizeof (int) * 15 );
S = malloc (sizeof (int *) * 10 );
P [1] [14] = 14014;
S [2] [6] = 159;
S [3] [7] = 98;
Printf ("% d, % d, % d", s [3] [7], s [2] [6], p [1] [14]);
Return 0;
}
The problem with the c pointer array is just a bit wrong.
Int a [4] [10], * p, * q [4];
Type of a: int [4] [10]. a is a one-dimensional array with four elements. Each element is a one-dimensional array with 10 int data types.
Essentially, the C language does not have multi-dimensional arrays! Please remember.
So the first element of a is a [0], and a is the address of the first element a [0], that is, a = & a [0]
Do not think that a is the address of the first element a [0] [0], but it is written as a = & a [0] [0], making a joke!
Remember: the first element of a is a [0]. You will not be confused when learning multi-dimensional arrays and pointers in the future!
P type: int *
Q type: int * [4]
What type is a [I? Very simple, int [10]! A one-dimensional array!
Q [I? Int * is also!
Important: why q [I] = a [I]? Don't you understand ?!
(Statement: Here is my personal originality analysis method, which is original and ugly !)
In fact, let's take a look at the definition of one-dimensional arrays and one-dimensional pointers.
Int A [10];
Int * Q;
Q = A; >>> do you understand? An array can be assigned to a one-dimensional pointer. (Only one-dimensional !)
However, a multi-digit group cannot be assigned multiple pointers. Remember!
Let's look at our question: q [I] = a [I]?
Because (a [I]) is a one-dimensional array (with parentheses, it is because a [I] is a whole. Remember: a is a so-called two-dimensional array !);
The (q [I]) (also as a whole) type is int *, that is, pointer. So you can assign values to the two!
To sum up, find the type method and remove the name!
For arrays, the int a [4] [10] And a types are removed from the int [4] [10]; a [I] type, remove int [10] from a [I]!
The pointer is actually the same.