Relationships of arrays, pointer arrays, array pointers, functions that return array pointers

Source: Internet
Author: User

Go directly to the code, and the explanation is clear.

C++primer.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace Std;int (*function (int i)) [10];//function that returns an array pointer int main () {cout << "arrays, pointer arrays, array pointers, functions that return array pointers:" <<endl<<endl;int arr[10]; Declares an array, an array element is Intint *p1[10];//declares an array, an array element is a pointer to int (*P2) [10];//declares a pointer to an array containing 10 shapes, and the P2 itself is a pointers for (int i = 0; i <; I + +) Arr[i] = i;for (int i = 0; i < ten; i++) cout<<arr[i]<< "";cout<<endl<< "arr:" <<arr<& lt;endl;//Output First address cout<< "&arr[0]:" <<&arr[0]<<endl;//output first element address cout<< "*arr:" << *arr<<endl;//output The first address points to the value of the element, which is the value of the first element cout<< "* (arr+1):" <<* (arr+1) <<endl<<endl;// By the addition of the pointer to the output element value cout<< "to P1 assigned to the address of the ARR element and output:" <<endl;for (int i = 0; i <; i++)///can also be &arr[i]p1[i] = arr+i; for (int i = 0; i < i++) cout<<p1[i]<< "" ";cout<<" the output array by referencing the elements in the P1 arr: "<<endl;for (int i = 0; I < 10; i++)//p1 is stored in the address cout<<*p1[i]<< "";cout<< "pointer to the array P2 assignment to ARAddress of R: "&LT;&LT;ENDL;P2 = &arr;//assigns a value to the pointer, P2 stores the address of arr cout<<" P2: "<<p2<<" << "*P2:" << Why is *p2<<endl;//the same? cout<< "P2[i]:" <<endl;for (int i = 0; I < 10; i++)//This visit is meaningless because it is out of bounds.      P2[i] Compiler interpreted as * (p2+i) cout<<p2[i]<< ""; And P2 is just an address, there is no meaning, it is followed by the element is not an array, with P2 no relationship cout<< "*p2[i]:" <<endl;for (int i = 0; I < 10; i++) cout<<*p2[i]<< "";//Ibid., referring to the above address, has crossed access, there is no meaning cout<< "(*P2) [i]:" <<endl;for (int i = 0; I < 10; i++) cout<< (*P2) [i]<< "";//equivalent to Arr[i]return 0;}

Paste the results:


Relationships of arrays, pointer arrays, array pointers, functions that return array 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.