C + + pointer array, array pointers, array names, and two-dimensional array techniques summary

Source: Internet
Author: User

This article mainly introduces C + + pointer array, array pointers, array names and two-dimensional array techniques to summarize, for in-depth understanding of C + + arrays and pointers is very important, the need for friends can refer to.

This paper analyzes some techniques of understanding C + + pointer array, array pointers, array names and two-dimensional arrays in detail. is a more important concept, I believe that everyone's C + + programming has a certain help.

first, about the array name

Suppose you have an array:

1 inta[3] = {1, 2, 3}

1. The array name represents the address of the first element of the array , note that it is not an array address (although the value is equal), is the first element of the array address, a is equivalent to &a[0];

A+1 is the address of the second element. than the first element address a (or &a[0]) exceeds the size of an integer pointer, here is 4 bytes (byte)

1 cout << a << endl;//会输出数组第一个元素地址。

2. Take the address symbol &.

&a is the address of the array, which is the address of the array, representing the address of the array as a whole. is not the address of the first element of the array (although their values are the same)

&a+1 than the address of an array &a the address size of an array, where it is 3*4 bytes

int * p = &a; This statement is not valid. The left pointer variable p is a pointer to an integral type, and the right side is an array (type is an array) address, not an array element (type is an integer) address, so you cannot assign a value.
The array pointers should be assigned (described below).

For the name of the array, remember the above two points.

second, about the pointer array

1. Definition

An array of pointers is an array of pointers, and the elements in the array are pointers (in contrast to an integer array, an integer array is an array of integers, and the elements in the array are integers).

int *ptr[3]; How to understand? According to the operator precedence, [] The priority is large, so ptr first with [3], indicating that PTR is an array, it is necessary to clarify the element type of the array, so the element type in the array is an integer pointer (int*), the size of the array is not necessarily required (define the array can be determined according to the number of elements initialized)

Ptr[0] is the 0th element of an array, which is an integer pointer.

Examples are as follows:


1 2 3 4 inta[3] = {1, 2, 3}; intx = 5; ptr[0] = &x;ptr[1] = &a[2];

2. How do I use it?

Used like a normal pointer. *PTR[0] is the value of the element that the 0th element (a pointer) points to, which is 5.


three, about array pointers

1. Definition

An array pointer is a pointer to an array, which is a pointer to an array (in contrast to an integer pointer, which is a pointer to an integral type, which is a pointer to an integer)

int (*PTR) [3]; How to understand? First look at the parentheses, *ptr shows that PTR is a pointer, and then the [] combination indicates that the pointer points to an array, the element of the array is an int


1 2 inta[3] = {1, 2, 3}; int(*ptr)[3] = a;//这条语句不成立。

The right A is the array name, remember the above said, the array name represents the address of the first element of the array, that is &a[0], the type of the array name is equivalent to an integer pointer (not known in fact) int *, because it points to the first element, the first element is an int

The type of the left PTR is int (*) [], an array pointer, a pointer to an array, not a pointer to an integral type, and cannot be assigned a value.


1 2 inta[3] = {1, 2, 3}; int(*ptr)[3] = &a;//正确。

Because a is an array, &a is the address of the array, remember what it says?

2. How do I use it?


1< /span>2 3 4 5 int a[3] = {1, 2, 3}; int (*PTR) [3] = &a; &NBSP;   cout << (*PTR) [0] << endl;  //output 1 cout << (*PTR) [1] << endl;  //output 2

It's a little hard to understand here. Don't compare the code with a bit.


1 2 3 4 inta[3] = {1, 2, 3}; intx = 5; int* p = &x; cout << *p << endl; //输出5

P is a pointer to an integral type, and *p is the value of the variable (integer x) to which the pointer is pointing. Similarly, PTR is a pointer to an array, and *ptr is the value of the variable (array a) to which it is pointing. (*PTR) [0] is the 0th element of the array.

Iv. about two-dimensional arrays

1. A two-dimensional array is an array whose elements are one-dimensional arrays . Keep this in mind, and then put the above on the line.


1 inta[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

Array name

A is the address of the first (or 0th better) element of the array, the first element is a one-dimensional array, a[0]------> {1, 2, 3}. A+1 is the address of the second element, which is the address of the second one-dimensional array, beyond the 3*4 bytes

&a is the address of the array, &a+1 is beyond the size of a two-dimensional array, exceeding 3 * 4 * 3 bytes.

Array pointers


1 int(*ptr)[3] = a; //正确。

Because a represents the address of the first element, the first element is a one-dimensional array, so a represents the address of a one-dimensional array, and the address of an array is assigned to the array pointer, which is set.


Five, Summary:

1. the array name represents the address of the first element of the array.

2.&a (A is an array) is the address of the array.

3. An array of pointers is an array whose elements are pointers.

4. The array pointer is a pointer that points to an array.

5. the elements of a two-dimensional array are one-dimensional arrays.


C + + pointer array, array pointers, array names, and two-dimensional array techniques summary

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.