Second, the key words of the pointer array and the difference between the set of pointers

Source: Internet
Author: User
Tags array definition

These two names are different, of course, the meanings they represent are different. I just started to see this scare, mainly Chinese is too broad and profound, the whole such abbreviation is too professional, people are around dizzy. It is easier to understand from the English explanation or the Chinese full name.

Pointer array: array of pointers, which is used to store pointers to arrays, which are arrays of elements that are pointers

Array pointer: A pointer to an array, which is a pointer to the array

Also note the differences in their usage, as illustrated below.

int* a[4] Pointer array

Expression: The elements in array A are INT-type pointers

Element means: *a[i] * (A[i]) is the same, because [] priority is higher than *

int (*a) [4] Array pointer

Represents: Pointer to array a

Element representation: (*A) [i]

Note: In practical applications, for pointer arrays, we often use this:

1 int* pInt; 2 pInt a[4];

This is the same as the meaning expressed in the pointer array definition above, except that the type transformation is taken.

The code demonstrates the following:

1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6     intc[4] = {1,2,3,4};7     int*a[4];//Array of pointers8     int(*B) [4];//Array Pointers9B = &C;Ten     //assigning an element in array C to array a One      for(inti =0; I <4; i++) A     { -A[i] = &C[i]; -     } the     //output look at the results -cout << *a[1] << Endl;//Output 2 is the right -cout << (*B) [2] << Endl;//Output 3 is the right -     return 0; +}

Note: The array pointer is defined, the pointer to the first address of the array, the pointer must be given an address, it is easy to make the mistake is, do not give B address, directly with (*B) [i]=c[i] to the array B element assignment, when the array pointer does not know where to point, debugging may be correct, but the runtime must have problems, Be aware of this when using pointers. But why does a not have to give him the address, the element of A is a pointer, in fact the for loop has already given the element in the array a address. But if you write *a[i]=c[i in the For Loop], this also goes wrong. In short, the definition of the pointer must know where the pointer points, or tragedy.

Similar to the pointer function and function pointers, we have encountered.

Second, the key words of the pointer array and the difference between the set of pointers

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.