Pointers and Arrays

Source: Internet
Author: User

Since pointer variables can point to variables, they can also point to array elements (placing an element's address in a pointer variable), and the so-called array element's pointer is the address of the array element.

int a[10];

int *p;

p=&a[0]; Assign the address of the a[0] element to the pointer variable p, which means p points to the No. 0 element of array A.

P=a; Equivalent to the previous sentence, the C language specifies that the array name represents the address of the first element in the array.

Attention:

(1) P+i and A+i is the address of a[i], that is, the actual address is calculated as a+i*d, because of the int type, so d=2.

(2) * (P+i) or * (a+i) is the element pointed to by p+i or a+i, i.e. a[i]

(3) Pointer variables to an array can also be labeled, such as P[i] and * (p+i) equivalent

So, by referencing an element, you can use the

1, subscript method, such as A[i]

2, pointer method, such as * (A+i) or * (P+i)

There are several issues to note when using pointer variables to point to array elements:

(1) You can point to different elements by changing the value of the pointer variable, eg:p++, this is possible.

(2) Note the current value of the pointer variable

(3) The size of the array in the above example is 10, but actually the pointer variable p can point to the memory unit after the array.

(4) Note the operation of the pointer variable (P=A)

I, p++, causes p to point to the next element, i.e. a[1], and if *p is executed, the value of a[1] is obtained.

II, *p++, because * and + + is the same priority, the binding direction is from right to left, so equivalent to * (p++), the role is to get *p first, and then make p+1

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.