C + + pointers and arrays

Source: Internet
Author: User

The pointer to an array is the starting address of the entire array in memory, and the pointer to an array element is the address of the storage unit of an element in the array.

Using pointer method to refer to array elements can make the target program of high quality, less memory and faster operation.

The C + + language stipulates that the array name represents the first address of the array, which is the address of the array No. 0 element. Such as

int a[10];
int *p;
p=&a[0];/*与p=a,是等价的*/

P+i or a+i are the storage addresses of a[i] elements, that is, they all point to the array's number-I element a[i]. So * (P+i) or * (A+i) is equivalent to A[i]. In fact, at compile time, the compiler processes the a[i] into * (a+i). Therefore, you can access the array elements using this method. Such as:

main()
{
int a[10];
int *p, i;
for(i=0;i<10;i++)
cin>>a[i];
p=a;
for(i=0;i<10;i++)
cout<<*(p+1);//等价于cout<<a[i];
}

Description

(1) p++ is the next element that points to an array element.

(2) *p++ operations, because the * and + + operation level is the same, and the binding property is from right to left, so the *p++ equivalent to * (p++), its role is to get the value of the element P points (that is, *p), and then p+1.

(3) * (p++) and * (++P) The role is different.

* (p++) is the first to take p to do * operation, and then P plus 1 (that points to the next operation); For example, in an array, p points to the third element, then the value of the third element is taken out (do the * operation), and then the P points to the next element.

* (++P) is to have P plus 1 first (even if p points to the next element), and then do the * operation. For example, in an array, p points to the third element, then the p is pointed to the fourth element, and then the value of the fourth element is taken out (do the * operation).

(4) (*p) + + means that the element value of P is plus 1, which is equivalent to a[0]++ in the example above.

(5) For the pointer--(self-subtraction) operation principle Ibid.

(6) Only pointer variables that point to an array element can be self added or reduced.

Array Masterpieces function parameters

Using an array name as a parameter of a function can be implemented in the following 4 ways:

(1) Parameters and arguments are array names

(2) The argument uses the array name, and the parameter is used as the pointer variable.

(3) Arguments and parameters are all used with pointer variables

(4) The argument uses the pointer variable, and the parameter array name

Array of pointers and pointers to pointers

1. Array of pointers

char* Name[3] = {"Fortran", "Basic", "Pascal"};

2, pointing pointer

Char **p;

**p is equivalent to * (*P), stating that the pointer variable p is pointing to a character pointer variable (a pointer variable that points to character data).

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.