C-language array pointers (pointers to arrays)

Source: Internet
Author: User

Note: The definition of an array pointer differs from the array of pointers

Reprint: http://c.biancheng.net/cpp/biancheng/view/162.html

Pointer variable pointing to multidimensional array element


① pointer variable pointing to an array element
Example 6.7 outputs the values of the elements of the two-dimensional array. The method used here is to point to each element with a pointer variable with a base type of integer, outputting their values one by one.

#include <iostream>using namespacestd;intMain () {inta[3][4]={1,3,5,7,9, One, -, the, -, +, +, at}; int*p;//p is a pointer variable with a base type of integral type     for(p=a[0];p <a[0]+ A;p + +) cout<<*p<<" "; cout<<Endl; return 0;}

The results of the operation are as follows:
1 3 5 7 9 11 13 15 17 19 21 23

A few notes about pointer variables that point to array elements:

    • P is a pointer to the integer data, in the For statement to P A[0], can also be written as "p=&a[0][0]".
    • The condition of the loop end is "p<a[0]+12", as long as the p<a[0]+12 is satisfied, the loop body is continued.
    • Execute "cout<<*p;" Output p The value of the column element currently referred to, and then execute p++ so that P points to the next column element.


② pointer variable pointing to a one-dimensional array of M elements
You can define a pointer variable that does not point to an integer element, but instead to a one-dimensional array that contains m elements. At this point, if the pointer variable p first points to a[0] (that is, p=&a[0]), then p+1 is not pointing to a[0][1], but the increment to a[1],p is measured in the length of the one-dimensional array, as shown in Figure 6.17.


Figure 6.17


"Example 6.8" outputs a two-dimensional array of values for any column element of any row.

#include <iostream>using namespacestd;intMain () {inta[3][4]={1,3,5,7,9, One, -, the, -, +, +, at}; int(*p) [4],i,j; CIN>>i>>J; P=A; cout<<* (* (p+i) +j) <<Endl; return 0;}

The operating conditions are as follows:
2 3
23

Because the "p=a" is executed, p points to the a[0]. So p+2 is the starting address of the row with the ordinal 2 in the two-dimensional array A (since p is a pointer variable to a one-dimensional array, so p plus 1 points to the next one-dimensional array), as shown in Figure 6.18. * (p+2) +3 is a array of 2 rows 3 column element addresses. * (* (p+2) +3) is the value of a[2][3].


Figure 6.18


3) Use pointers to arrays as function parameters
A one-dimensional array name can be passed as a function parameter, and a multidimensional array name can also be passed as a function parameter.

Example 6.9 outputs the values of the elements of the two-dimensional array. The title is the same as example 6.7, but the topic uses a function to implement the output, using multidimensional array function parameters.

#include <iostream>using namespacestd;intMain () {voidOutputint(*p) [4]);//function Declaration    inta[3][4]={1,3,5,7,9, One, -, the, -, +, +, at};  Output (a); //multidimensional array masterpiece function parameters    return 0;}voidOutputint(*p) [4])//A parameter is a pointer variable that points to a one-dimensional array{    inti,j;  for(i=0;i<3; i++)         for(j=0;j<4; j + +) cout<<* (* (p+i) +j) <<" "; cout<<Endl;}

The operating conditions are as follows:
1 3 5 7 9 11 13 15 17 19 21 23

C-language array pointers (pointers to 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.