C + + arrays, pointers, references, and composite types of three

Source: Internet
Author: User
Tags introductions

C + + arrays, pointers, references, and composite types of three

There is no separate introduction to these three, only the combination types that use these three components are introduced.

One, 22 combination

There are 9 types of 22 combinations of these three:

Array

Pointer

Reference

Array

Array of arrays

Pointers to arrays

References to arrays

Pointer

Array of pointers

Pointer to pointers

Reference to Pointers

Reference

Array of references

Pointers to references

References referenced by

1 pointers, arraysof arrays, pointers to arrays, arrays of pointers

Pointers to pointers (two-dimensional pointers), arrays of arrays (two-dimensional arrays), arrays of pointers and pointers to arrays are not introduced here, many blog post books are described in detail. They are in the form of:

Pointer's pointer: int** a = NULL;

Array of arrays: int a[2][3] = {0};

Pointer to array:int (*a) [10];//a pointer to an int array with ten elements

Array of pointers:int *a[10];//a is an array with ten elements, and the type of the element is (int *)

The following is the topic of discussion, when the " reference " type is introduced in C + + , things are a little more complicated. When I try to use an array of pointers, I find that I will pass the array to the function in a reference way, and for the first time I find that I do not define the combination type of the three, and find some information to summarize the composite types of these three into this article.

2.array Reference, array of references

When you combine references with arrays, references and pointers, there are some introductions on the web, in the form of:

References to arrays:

int a[3] = {n/a};

int (&b) [3]=a;

are pointers to and from arrays very similar?? So is the referenced array supposed to be the following, too?

int h = 4;

int &c = h;

int &d = a[1];

int &e = a[1];

int &f[3] = {c,d,e};//error

But this is wrong ,C + + cannot define the referenced array, the compiler does not pass. The classic explanation ("C + + Primer") is that references are not objects. My own idea is that the reference passes not only the value, but also the address information of the variable, and that the variables pointed to by multiple references are not necessarily contiguous in memory (as in the above example , H and a[1]). Not necessarily different addresses (such as the references D and e in the previous example point to the same variable), but the element address of the array must be contiguous.

Great, there will be no troublesome compound type made up of " referenced arrays " ! !

3.pointer Reference, reference pointer

Pointers to references and pointers to these two are also a lot of introductions,

Reference to the pointer:

int a = 0;

int * p = &a;

int* &b = p;

Referenced pointers: As with the referenced array, there is no such thing as the classic explanation: the reference is not an object.

So we're going to get rid of a combination that will never have a composite type that's made up of reference pointers .

4. Referencedreferences

Reference, you can of course do this:

int a = 0;

int &b = A;

int &c = b;

However, you cannot define such a reference:

int a = 0;

int &&b = A;//error

int & (&B) = A;//error

(The pointer cannot do this:int * * p = &&a;//error, not explained)

In short, forget the above two kinds of wrong way:

First, the compilation information given by g++ is as follows:

error:expected Unqualified-id before ' && ' token

int &&p = A;

Second, the compiler information given by g++ is as follows:

Error:cannot declare reference to ' int& ', which are not a typedef or a template type argument

int & (&P) = A;

All right, so we can get rid of 9 of the 3 kinds When we do three combinations .

Let's start by listing the two combinations you can use:

Array

Pointer

Reference

Array

Array of arrays

Pointers to arrays

References to arrays

Pointer

Array of pointers

Pointer to pointers

Reference to Pointers

Reference

combination of two or three persons

1 pointer to pointer, array of array of arrays

Array

Pointer

Reference

Array of arrays

An array of arrays of array

Pointer to an array of arrays

A reference to an array of arrays

Pointers to arrays

Array of pointers to arrays

Pointer to an array of pointers

A reference to an array of pointers

References to arrays

Array of references to arrays

Pointer to an array of references

Reference to an array of references

Array of pointers

Array of pointers to arrays

Pointer to an array of pointers

A reference to an array of pointers

Pointer to pointers

Arrays of pointers to pointers

Pointer to pointer to pointers

A reference to a pointer pointer

Reference to Pointers

Array of pointers to references

Pointer to a reference to a pointer

Reference to a reference to a pointer

1, non-existent type

An array of reference arrays, an array of reference pointers, an array of reference references, pointers to referenced arrays, pointers to reference pointers, pointers to references to references to these composite types do not exist, refer to 22 combinations.

2 pointer to pointer, array of array of arrays

Pointer to a pointer to an array of arrays of arrays of pointers: three-dimensional pointers and arrays, each of which are in the form:

int * * * p = NULL;

int a[2][4][3] = {0};

3.array of pointer pointers

Pointer to an array of pointers: an array of pointers, after all pointers are pointers, which are in the form of:

int * * A[2];//A is an array with two elements, and the type of the element is int**

4.reference to pointer pointer

Reference to pointer pointer: reference to the pointer, which is in the following form:

int **&b = a[0];

5.pointers to arrays of arrays

Array of pointers to arrays: pointers to arrays, after all arrays are arrays, which are in the form of:

int (*p) [2][3] = NULL;//P is a pointer to a 2 row 3 column element as int array of type.

6. Reference to array ofarrays

A reference to an array of arrays: a reference to an array of references, and a pointer to an array of arrays similar to the following form:

int a[2][3] = {0};

int (&r) [2][3] = A;

7, array of pointers to arrays

I do not read a lot of programs, have not seen such use of the program, I naturally did not use it, but still do not prevent us from giving the definition of such objects:

The array of pointers is so defined:

int *b[2] = {Null,null};

In fact, it's no problem to write this:

int * (b) [2] = {Null,null};

The pointer in the array of pointers to the int type, now to point to the int array type, such as int [2], as long as the b replaced by a int the array is available:

int * (b[2]) [2] = {Null,null};

Well, the array type of pointers to arrays is defined, which is an array of two pointers that point to An array with two elements of type int.

I think later if you want to define an array of pointers to arrays of pointers to Arrays , you can also do this by stepping into this compound type.

int * (* (a[2]) [2]) [2] = {Null,null};

It would be difficult to choose a pointer from an array to start the derivation. I was not introduced here.

8. Pointerto an array of pointers

The pointer type of the array is so defined: int (*a) [2] = NULL;

You can also write this:Int (* (a)) [2] = NULL;

So if you want to take the address again for a , then replace a with *a , so the pointer object of the array pointer is defined like this:

Int (* (*a)) [2] = NULL;

In contrast to an array of pointers to the previous array, you might have the following questions:

The pointer pointer is this:int * * a = NULL;

Based on the derivation of an array of pointers to arrays, pointers to arrays of pointers should be drawn like this:

int * * (a) = NULL;

int * * (a[2]) =null; Error

Unfortunately, the compilation does not pass. I don't know any reason for the moment.

Comparing the derivation of these two composite types, we do not seem to find any very fixed laws. Everything is a experience of experience. And so on, you should see the rules later.

9.array of pointers to arrays

This is easier because the array of pointers is this:int *a[2] = {Null,null};

So it's easy to write:

int *a[2][2] = {0};

A pointer to an array of pointers

Array of pointers:int *a[2] = {0};

ALSO:int * (a) [2] = {0};

So, pointer to an array of pointers:int * (*A) [2] = NULL;

I do not know how to summarize the rules, but I write an example of my experience should be better understood.

Combining the above several composite types, we can combine a very insane compound type in C or C + + , for example: pointer to an array of pointers to an array of arrays of pointers to a pointer to an array pointer

My approach is this:

From the right number, find the first " array ":

Pointer to an array of arrays of pointers to an array of pointers to the pointer of

What kind of array? Array of pointers

Pointer to an array of arrays of pointers to an array of pointers to the pointer of

So first define an array of pointers

int * (a) [2] = {0};

So What is a pointer to a? Pointer to an array? Or a pointer to an array of arrays? We take all the modifier " arrays " out of the array , so it's a pointer to an array of arrays:

Pointer to an array of arrays of pointers to an array of pointers to the pointer of

int * (a[2][2]) [2] = {0};

So What is in the array of a array? Pointer? Pointer to pointers? We take all the modifier " pointers " out, so we're holding pointers to the pointer:

Pointer to an array of arrays of pointers to an array of pointers to the pointer of

int * (**a[2][2]) [2] = {0};

So What kind of pointer is a now? Pointers to arrays:

Pointer to an array of arrays of pointers to an array of pointers to the pointer of

int * (* * (a[2]) [2][2]) [2] = {0};

Well, the first half is pushed out, and there's the back part:

Now we know that a is an array, and we want to take pointers to it as well:

Pointer to an array of arrays of pointers to an array of pointers to the pointer of

int * (* * ((**A) [2]) [2][2]) [2] = NULL;

Complete!!!!

Well, I didn't test it.

This is complicated enough, but this is not yet added to the function pointer ...

Okay, here's the quote.

Areference to a pointer to an array

Obviously, the pointer to the array is written first, so

int (*p) [2] = NULL;

Reference to the pointer, and the reference to the pointer, so

int (*&R) [2] = p;

Areference to an array of pointers

Obviously, the array of pointers is written first, so

int *p[2] = {0};

Reference to the array, so

int * (&R) [2] = p;// This bracket cannot be saved

Summarize the composite types of the three combinations available :

Array

Pointer

Reference

Array of arrays

An array of arrays of array

Pointer to an array of arrays

A reference to an array of arrays

Pointers to arrays

Array of pointers to arrays

Pointer to an array of pointers

A reference to an array of pointers

References to arrays

Array of pointers

Array of pointers to arrays

Pointer to an array of pointers

A reference to an array of pointers

Pointer to pointers

Arrays of pointers to pointers

Pointer to pointer to pointers

A reference to a pointer pointer

Reference to Pointers

C + + arrays, pointers, references, and composite types of three

Related Article

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.