pointer arrays and arrays of pointers (handling)

Source: Internet
Author: User

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

int* a[4] means: The elements in array A are all int pointers, and the element means: *a[i] * (a[i]) is the same, because [] precedence is higher than *

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

int (*a) [4] means: A pointer to array a, which is represented by an element: (*a) [i]

1#include <iostream>2  3 using namespacestd;4  5 intMain ()6 {7     intc[4]={1,2,3,4};8 9     int*a[4];//Array of pointersTen     int(*b) [4];//Array Pointers one  ab=&c; -  -     //assigning an element in array C to array a the      for(intI=0;i<4; i++) -     { -a[i]=&c[i]; -     } +  -     //output look at the results +cout<<*a[1]<<endl;//Output 2 is the right acout<< (*b) [2]<<endl;//Output 3 is the right at      -     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.

pointer arrays and arrays of pointers (handling)

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.