C Language Basics Tutorial (iv) Pointers, structures, unions, and enumerations (4)

Source: Internet
Author: User
Tags integer
2.1. Pointers to array elements
We define an integer array and a pointer variable that points to an integral type:
int a[10], *p;
As described earlier, you can point an integer pointer p to any element in the array, assuming that the assignment operation
p=&a[0];
At this point, p points to the No. 0 element in the array, a[0], and the pointer variable p contains the address of the array element a[0], because the array elements are stored continuously in memory, so we can indirectly access any element of the array through the pointer variable p and its related operations.
In Turbo C, the array name is the address of the No. 0 element of the array, so the following two statements are equivalent
p=&a[0];
P=a;
According to the address operation rule, a+1 is the address of a[1], A+i is the address of a[i].
Here we use pointers to the address of the array element and several representations of the content.
(1). P+i and A+i all represent the addresses of a[i], or they all point to the array number-I element, which points to a[i].
(2). * (P+i) and * (A+i) All represent the contents of the objects referred to P+i and a+i, that is, a[i].
(3). A pointer to an array element can also represent the form of a group, that is, it allows pointer variables to be subscript, such as p[i] and * (p+i) equivalent.
if: p=a+5;
Then p[2] is equivalent to * (p+2), because P points to a[5], so p[2] is equivalent to a[7. and p[-3] is equivalent to * (p-3), it means a[2].
Related Article

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.