Differences between array pointers and pointer Arrays

Source: Internet
Author: User

The two names have different meanings. I was shocked when I first saw it. The main reason is that Chinese is too broad and profound, and such short expressions are too professional, and people are confused. It is easy to understand from the English explanation or full Chinese name.

Pointer array: array of pointers, that is, the array used to store pointers, that is, the array elements are pointers.

Array pointer: a pointer to an array, that is, the pointer to the array

Note the differences in their usage. The following is an example.

Int * A [4] pointer Array

All elements in array a are int-type pointers.

Element representation: * A [I] * (a [I]) is the same, because [] has a higher priority *

INT (* A) [4] array pointer

Pointer to array

Element representation: (* A) [I]

Note: in practical applications, we often use pointer arrays as follows:

?
12 typedef int* pInt;pInt a[4];

This is the same as the expression in the above pointer array definition, except that the type conversion is adopted.

The code is demonstrated as follows:

?
1234567891011121314151617181920 #include <iostream> using namespace std; int main(){int c[4]={1,2,3,4};int *a[4]; // Pointer Arrayint (*b)[4]; // Array pointerb=&c;// Assign the element in array C to arrayfor(int i=0;i<4;i++){a[i]=&c[i];}// Output the resultcout<<*a[1]<<endl; // Output 2cout<<(*b)[2]<<endl; // Output 3return 0;}

Note: An array pointer is defined. The Pointer Points to the first address of the array. You must specify an address for the pointer. The easy mistake is not to give B an address, directly assign values to the elements in array B using (* B) [I] = C [I]. At this time, the array pointer does not know where to point. This may be true during debugging, but there must be a problem during the running. Pay attention to this problem when using pointers. But why does a not need to give him an address? The element of A is a pointer. In fact, in the for loop, an address has been specified for the element of array. However, if you write * A [I] = C [I] In the for loop, the problem also occurs. In a word, if a pointer is defined, you must know where the Pointer Points. Otherwise, it would be a tragedy.

There are also pointer functions and function pointers. Let's talk about it later.

----------------------------------------------

 

 

An array pointer (also known as a row pointer) defines int (* P) [N]; () with a high priority. P is a pointer that points to an integer one-dimensional array, the length of this one-dimensional array is N, or the step of P. That is to say, when p + 1 is executed, P must span the length of N integer data.

 

To assign a two-dimensional array to a pointer, the value should be: int A [3] [4]; int (* P) [4]; // This statement defines an array pointer pointing to a one-dimensional array containing four elements. P = A; // assign the first address of the Two-dimensional array to P, that is, a [0] Or & A [0] [0] P ++; // after the statement is executed, P = p + 1; p crosses row A [0] [] and points to row A [1] []

 

Therefore, an array pointer is also called a pointer to a one-dimensional array or a row pointer.

 

The pointer array defines int * P [N]; [] with a high priority. It is first combined with P into an array, and then int * indicates that this is an integer pointer array, it has n pointer-type array elements. The execution of p + 1 here is incorrect, so the value assignment is also incorrect: P = A; Because p is an unknown representation, only P [0], p [1], p [2]... P [n-1], and they are pointer variables that can be used to store variable addresses. But it can be like this * P = A; here * P represents the value of the first element of the pointer array and the value of the first address of. To assign a two-dimensional array to a pointer array: int * P [3]; int A [3] [4]; for (I = 0; I <3; I ++) P [I] = A [I]; here int * P [3] indicates that three pointer variables exist in the memory of a one-dimensional array, values are P [0], p [1], and P [2]. Therefore, values must be assigned respectively.

 

In this way, the difference between the two is suddenly clear. The array pointer is just a pointer variable. It seems that the C language is specifically used to point to a two-dimensional array, which occupies the storage space of a pointer in the memory. A pointer array contains multiple pointer variables in memory and occupies the storage space of multiple pointers. It also needs to be noted that when it is used to point to a two-dimensional array, its reference is the same as the array name reference. For example, to represent an element in column J of row I in the array: * (p [I] + J), * (p + I) + J), (* (p + I) [J], p [I] [J]

 

 

 

Priority: ()> []> *

 

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.