Supplementary content: C ++ language array pointer and pointer array thoroughly analyzed

zhaozj2021-02-16  56

The last time we mainly explain the array, pointer, pointer array, array pointer these very basic concepts, but one thing I didn't mention it, that is, we use typedef to define an array of array pointers, this name is very I have a mouth, but we will see the specific procedure.

File: // ---------- Array ------------- INT A [2] = {10, 20}; int b [2] = {30, 40}; file: // Simple points pointing to the pointer INT (* p2arr) [2] = & A;

File: // --------- Note the changes here --------

File: // We declare a simple type P2ARR. You can describe that file: // p2arr is a user-defined type. Her function description file: // is used to describe an array of integers, this array can only contain two elements typef int (* p2arr) [2];

P2ARR PP = & B; PP = p2arr; // Type

File: / / Here we define the array P2ARR AP2A [2] = {& A, & B} to point to array pointers;

File: // If we use the following way to write is wrong, the reason is to analyze file: // p2arr ap2a [2] = {a, b}; // ------ Error Write ----- -

--------- There is also a little attention ---------------- Someone can say, define the array of digital resistance pointers, the following form seems to be available Arrived. INT (* p2arr) [2] [2]; 咋, it seems, but think about it. It turns out that it is defined a pointer. This pointer is a pointer to the two-dimensional array or an array pointer, not an array pointer array. This should be very easy to understand.

The last time I told the int [] array declaration with the INT * type to convert an example in the function parameters. Here is a little supplement, we actually still unclear the representation of the C compiler level, that is, we don't know what structure in which the compiler is used to represent array. Therefore, these conversions can only be converted, we can't have more assumptions.

---------- My imagination, my guess -------- I am doing this, C uses the form similar to the Array class in Java. Of course, there are many of the C languages, such as Operator *, Operator -> and so on, but the use of these type conversion operators, only the compiler is considered needed, because the array represents itself is the internal structure of the compiler. Well. At present, we can only think that int [] to int * conversions can only occur in the case of function parameters.

Look at the procedure below. #include using namespace std; void change (int Pa [】, int index) {pa [index] = 200;} int main () {Int a [3] = {1, 2, 3}; Change A, 0); // Here the compiler has converted INT A [3] to the INT * pointer to the function file: // This is the language implementation details, please refer to "The C Programming Language" cout << a [ 0]; RETURN 0;}

But we have the labeled line // p2arr ap2a [2] = {a, b}; // ------ Error Write -------

It should be understood that the array is an array, the pointer is a pointer, although a certain conversion will be performed when the compiler needs, but we can't think that those conversions are "think of course". This line above, the compiler does not turn to us, so we have to do it. // p2arr ap2a [2] = {& a, & b}; // ------ Correct writing -------

These words are mainly illustrated, and some behavior is completed by the compiler, and there are some secrets. But we have to know the compiler, those who have to do it. What is important is to learn how to use the typedef definition type.

转载请注明原文地址:https://www.9cbs.com/read-28011.html

New Post(0)