Recently, the airport in the forum often sees discussions on array pointers and pointer arrays. This is an indispensable step in learning the language such as C , but it is very useful but it is also very difficult to use, so it is not very easy to learn. Recently, I have no projects can do, so I will write my own understanding of these aspects, for peer reference, and you can also understand your own mistakes in the sun, accept the baptism of everyone.
File: // Powered by Zostapo file: //dertyang@263.net
################################# ## basic knowledge ## ########### #######################################
Of course, we have started from the simplest built-in type, and finally I will do some promotion. Let's take a look at the basic form, we start here!
-------------- Pointer -------------- INT A = 10; int * p = & a;
------------- pointer of the pointer ----------- INT b = 20; int * p = & b; int ** p2p = & p;
------------- Simple array --------------- int C [10]; // integer array, contains 10 integer elements File: / / That is to say that every element is integer -------------- Pointer array ------------------ int * P [10]; // Pointer array contains 10 pointer elements File: // That is to say that every element is a pointer --------------------- ------------- int (* p) [10]; // array pointer, this pointer can be used to point to File: // An array of integers containing 10 elements
These simple forms are what we must first understand, this is basic knowledge. At the same time, we have to get a very important knowledge tips from above: the part of the variable declaration on the C language level, the priority of the suffix binding variable is higher than the prefix. Looking at the last two of our examples, we don't have to change it in order to achieve array pointers. We use () to achieve priority changes, implement the declaration of array pointers.
#################################################################################################################################################################################################################################################################################### ##############################
Array, array of pointers, arrays of pointers, too much concept. When I accepted more than a concept, I would like to simply put these complicated things. Because I am too lazy, the concept is simplified, remember is easier. So we have to know the essence of these concepts here. This simplifies the concept and reducing the difficulty of memory.
First look at a program. #include
COUT << "Declaration [int vint = 10] type ==" << TypeId (VINT) .Name () << endl; cout << "Declaration [arr [2] = {10, 20}] type ==" << TypeId (arr) .name () << endl; cout << "DECLATION [INT * P = & VINT] type ==" << typeId (p) .name () << Endl; cout << "Declaration [ INT ** P2P = & P] Type == "<< TypeId (p2p) .name () << endl; cout <<" Declaration [INT * PARR [2] = {& VINT, & VINT}] type == << TYPEID (PARR) .Name () << end1; cout << "Declaration [int (* p2arr) [2] = & arr] type ==" << TypeId (p2arr) .Name () << endl; returnograph; }
The result of the run is as follows: (I added the line number #xx) # 01 declaration [int vint = 10] type == int # 02 declaration [Arr [2] = {10, 20}] type == int * # 03 Declaration [INT * P = & VINT] type == int * # 04 declaration [int ** p2p = & p] type == int * * # 05 declaration [int * parr [2] = {& vint, & vint}] type = = int ** # 06 declaration [int (* p2arr) [2] = & arr] type == int (*) [2]
Now let's analyze the results. Because we already have the basic knowledge of the first part, we can now clearly distinguish our statements. There are two important parts here, and we are just talking about things, how the compiler is not implemented here.
-------- # 02: Array --------------
Now look # 02, what did you think of? In the compiler, an array is just a corresponding type of pointer type. When we pass the array to the function as a parameter, it passes the pointer, so we can use parameters to modify array elements. This conversion is to complete the compiler.
Void f (int []); int A [2] = {10, 20}; f (a); // This line is equivalent to function conversion F (int * P) completed by the compiler
That is to say, the compiler automatically completes the transformation of int [] type to int *, pay attention to the completion of the compiler, or it is said that the language itself is implemented, and we only accept it.
------- # 05: Array in the pointer ---------------
The internal representation of the compiler of the pointer array is also a pointer to the corresponding type.
------ # 06: Arrange pointer -------------------------------------------------------------------------------------------------------------------------- The compiler (or language itself) has an array pointer to this internal representation. Due to the language of the type of C language, there are other transformations of implicit types)
So our way below is not compiled. {file: // --------- Compile cannot pass ------------ int Arr [3] = {10, 20}; // Note is 3 elements Array int (* p2arr) [2] = & arr; // Note is pointing to 2 element array pointer file: // --------- Compile cannot pass ----------- ---} ############################################################################################################################################################################################################################################################################################ ###################
Through the contents of the above two sub-sections, everyone should basically understand, array, pointer, pointer array, array pointer, how is it going.
----------- Supplementation ----------------------- Conversions on arrays and pointers, and our use of pointers ( , -), etc., operate arrays, is based on arrays in memory.
But when we use "iterators", the situation is different. This issue does not discuss this article.
----------- Supplement ---------------------
However, the C language itself has a lot of strange places (because C should take into account compatible with the C language and the old C version). These nature features of the built-in type will have a small change in the function section, but if you understand what the compiler has done, you will not be too strange. However, I will say next time on the content of the function.
Now return to the above content. We still talk about the types of buildings here. Obviously the same type of variable can be assigned each other. However, there are certain other situations, such as the type of width, and the inheritance system issues, etc.
Of course, different types generally cannot assign each other, of course, the exception here is forced transformation, the inheritance system, etc.
See why the following programs will run. I also take the following procedures here as a summary of today:
#include
------------- Function pointer part is written back --------------------