Differences between pointer arrays and array pointers

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:

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:

# Include <iostream> using namespace STD; int main () {int C [4] = {1, 2, 4}; int * A [4]; // pointer array int (* B) [4]; // array pointer B = & C; // assign the element in array C to array afor (INT I = 0; I <4; I ++) {A [I] = & C [I] ;}// the output shows the result cout <* A [1] <Endl; // output 2 is for cout <(* B) [2] <Endl; // output 3 is for return 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.

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.