PART6 arrays, pointers and strings 6.6 pointers and arrays

Source: Internet
Author: User

An array is a contiguous set of data, which can be traversed by the arithmetic operation of the pointer, which in turn points to each element of the array.

Defines a pointer to an array element:

    int a[], *PA;    PA=&a[0]; or pa=a;

After the above definition and assignment:
*pa is a[0],* (pa+1) is a[1], ..., * (Pa+i) is a[i].
A[i], * (pa+i), * (A+i), pa[i] are equivalent.

//example 6-7 (1) using array names and subscripts to access array elements#include <iostream>using namespacestd;intMain () {inta[Ten] = {1,2,3,4,5,6,7,8,9,0};  for(inti =0; I <Ten; i++) cout<< A[i] <<" "; cout<<Endl; return 0;}//Example 6-7 (2) accessing array elements using array name and pointer operations#include <iostream>using namespacestd;intMain () {inta[Ten] = {1,2,3,4,5,6,7,8,9,0};  for(inti =0; I <Ten; i++) cout<< * (a+i) <<" "; cout<<Endl; return 0;}//Example 6-7 (3) using pointer variables to access array elements#include <iostream>using namespacestd;intMain () {inta[Ten] = {1,2,3,4,5,6,7,8,9,0};  for(int*p = A; p< (A +Ten); p++) cout<< *p <<" "; cout<<Endl; return 0;}

Array of pointers: The elements of an array are pointer-type

//example 6-8 using a pointer array to hold a matrix#include <iostream>using namespacestd;intMain () {intLine1[] = {1,0,0};//three rows of the matrix    intLine2[] = {0,1,0}; intLine3[] = {0,0,1}; int*pline[3] = {Line1,line2,line3};//defines an array of integer pointers and initializescout <<"Matrix Test:"<<Endl;  for(inti =0; I <3; i++){         for(intj =0; J <3; J + +) cout<< Pline[i][j] <<" "; cout<<Endl; }    return 0;}

PART6 arrays, pointers and strings 6.6 pointers and arrays

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.