Tips for understanding pointer arrays, array pointers, array names, and two-dimensional arrays ., Pointer two-dimensional array

Source: Internet
Author: User

Tips for understanding pointer arrays, array pointers, array names, and two-dimensional arrays ., Pointer two-dimensional array

Tips for understanding pointer arrays, array pointers, array names, and two-dimensional arrays.


/************* About the array name ***************/

Int a [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), it is the address of the first element of the array, and a is equivalent to & a [0];

A + 1 is the address of the second element. The size of an integer pointer is greater than that of the first element address a (OR & a [0]). Here it is 4 bytes (byte)

Cout <a <endl; outputs the address of the first element in the array.

2. Get the address Symbol &.

& A is the address of the array. Note that it is the address of the array, indicating the overall address of the array. Not the address of the first element of the array (although their values are the same)

& A + 1 represents the address size of an array, which is 3*4 bytes.

Int * p = & a; this statement is not valid. When the left pointer Variable p points to the integer pointer, and the right side is the address of the array (the type is an array), not the address of the array element (the type is an integer), so the value cannot be assigned.
Assign a value to the array pointer (as described below ).


For the array name, remember the preceding two points.




/************* About the pointer array ***************/

Int a [3] = {1, 2, 3 };

1. Definition

A pointer array is an array for storing pointers. elements in an array are pointers (compared with an integer array, an integer array is an array for storing integers, and the elements in an array are integers)

Int * ptr [3]; how to understand? According to the operator priority, [] has a large priority. Therefore, ptr is first combined with [3] to indicate that ptr is an array and the element type of the array must be specified, therefore, the element type in the array is an integer pointer (int *), and the size of the array is not necessarily required (the array can be defined based on the number of elements initialized)

Ptr [0] is the zero element of the array. It is an integer pointer.

Int x = 5;
Ptr [0] = & x;
Ptr [1] = & a [2];

2. How to use it?

It is used like a normal pointer. * Ptr [0] is the value of the element pointed to by the zero element (a pointer). Here is 5.



/************* About array pointers ***************/

Int a [3] = {1, 2, 3 };

1. Definition

An array pointer is a pointer to an array. It is a pointer to an array (compared to an integer pointer, It is a pointer to an integer)

Int (* ptr) [3]; how to understand? First look at the parentheses. * ptr indicates that ptr is a pointer, and then combined with [] indicates that this pointer points to an array, and the element of the array is int.

Int (* ptr) [3] = a; this statement is not true.

A on the right is the array name. Do you still remember the above? 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 (I don't know if it is actually) int *, because it points to the first element, and the first element is int

The ptr on the left is of the int (*) [] type. It is an array pointer to an array. It is not an integer pointer and cannot be assigned a value.

Int (* ptr) [3] = & a; correct.

Because a is an array, & a is the address of the array. Do you still remember what I mentioned above?


2. How to use it?

Int (* ptr) [3] = &;

Cout <(* ptr) [0] <endl; // output 1
Cout <(* ptr) [1] <endl; // output 2


This is a bit hard to understand. Do not compare the code.

Int x = 5;
Int * p = & x;
Cout <* p <endl; // output 5

P is a pointer to an integer. * p is the value of the variable (integer x) pointed. Similarly, ptr is a pointer to an array. * ptr is the value of the variable (array. (* Ptr) [0] is the zeroth element of the array.




/************ About two-dimensional arrays ***************/

1. A two-dimensional array is an array whose elements are a one-dimensional array. Keep this in mind, and then set the above.

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


Array name

A is the address of the first (or 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, that is, the address of the second one-dimensional array, which exceeds 3*4 bytes.

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



Array pointer

Int (* ptr) [3] = a; correct.

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



Summary:

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

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

3. the pointer array 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 language two-dimensional array and pointer, I want to know why * (a + 1) and the displayed a + 1, the given address is the same, how to understand?

Two-dimensional arrays are two-layer pointers.
* (A + I) + j) indicates the j Data of row I. Note that there are two *
If you do not understand it, remember to apply it.

One X is a pointer.
So ij is a pointer.

A + 1 is a pointer that represents the first address of the 1st rows (from 0 in array c)

In a two-dimensional array,
The three pointers a, a [0], & a [0] [0] are the values of the first address of a, but they are different in c.
A Indicates the first address of the Two-dimensional array.
A [0] indicates the first address of row 0th.
& A [0] [0] indicates the address of the 0th elements in the first row.

C language: Using pointers to reference multi-dimensional arrays, two-dimensional array a [0] is equivalent to * (a + 0). How can this problem be solved?

Indicates the first address of the 0th + 1 line.

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.