Supplement: thorough analysis of array pointers and pointer arrays in C ++

Source: Internet
Author: User

The last time, we mainly described the basic concepts of arrays, pointers, pointer arrays, and array pointers,
But one thing I did not mention last time is that we use typedef to define a pointer to an array.
Array, this is not very easy to call, but we can see the specific program clearly.

File: // ---------- array --------------
Int A [2] = {10, 20 };
Int B [2] = {30, 40 };
File: // simple pointer to two element arrays
INT (* p2arr) [2] = &;

File: // --------- note the changes here --------

File: // here we declare the simple type p2arr. This can be described as follows:
File: // p2arr is a user-defined type. Her Function Description
File: // is used to describe an integer array. This array can contain only two elements.
Typedef int (* p2arr) [2];

P2arr pp = & B;
Pp = p2arr; // consistent type

File: // here we define an array pointing to an array pointer
P2arr ap2a [2] = {& A, & B };

File: // if the following write rule is incorrect, the cause is as follows:
File: // p2arr ap2a [2] = {a, B}; // ------ incorrect syntax -------

--------- Another note ----------------
Someone can say that the following form defines the array of the Data resistance pointer.
It seems that this can also be done.
INT (* p2arr) [2] [2];
At first glance, it seems yes, but think about it carefully. Previously defined
A pointer to a two-dimensional array or
Array pointer instead of array pointer array. This should be easy to understand.

The last time I talked about how to convert int [] array declaration and int * types to function parameters.
Here is a supplement. We do not know how to use arrays in the C ++ compiler.
Representation, that is, we do not know what structure form the compiler uses to represent arrays. So
The preceding conversions can only be converted. We cannot have more assumptions.

---------- My imagination, my conjecture --------
C ++ manages arrays in a way similar to the array class in Java. Of course
There are many features of the C ++ language, such as operator *, operator-> and other heavy loads, but these types
The use of conversion operators is called only when the compiler deems it necessary, because the array represents itself.
It is the internal structure of the compiler. Currently, we can only consider the use of function parameters.
Int [] to int.

See the following program.
# Include <iostream>
Using namespace STD;
Void change (INT pa [], int index)
{
Pa [Index] = 200;
}
Int main ()
{
Int A [3] = {1, 2, 3 };
Change (A, 0); // the compiler has converted int A [3] to an int * pointer and passed it to the function.
File: // This is the language implementation details. Please refer to the C ++ Programming Language
Cout <A [0];
Return 0;
}

But the above annotation line
// P2arr ap2a [2] = {a, B}; // ------ incorrect syntax -------

It should be understood as follows: arrays are arrays, pointers are pointers, although the compiler needs
But we cannot take those conversions for granted.
In the above line, the compiler does not convert the type for us, so we have to do it ourselves.

// P2arr ap2a [2] ={& A, & B}; // ------ correct syntax -------

The above text mainly explains that some behaviors are completed by the compiler, and some
Secret. But we need to know what the compiler has done, and those are done by ourselves. Important,
That is, we learn how to simply use typedef to define the type.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.