The relationship between pointers and arrays based on the C language pointer, And the C language pointer Array

Source: Internet
Author: User

The relationship between pointers and arrays based on the C language pointer, And the C language pointer Array

About arrays and pointers:

The base address of the array is the starting position for storing the array in the memory. It is the address of the first element in the array (subscript: 0). Therefore, the array name is an address, that is, the pointer value.

Int a [10], * p is equivalent to p = a and p = & a [0]

P = a + 1 and p = & a [1] are equivalent

* (A + I) is equivalent to a [I]

* (P + I) is equivalent to p [I]

Array length: sizeof (a)/sizeof (int)

Pointer variables are involved in displacement operations such as p + 1 and p + 2. moving a few bytes after each plus 1 is dependent on the pointer variable type (equivalent to the array badge + 1)

Int * p + 1 Move 4 bytes

Char * p + 1 Move 1 byte

For example:

Void sum (int * p, int n) {int sum = 0; for (int I = 0; I <n; I ++) {sum = sum + * (p + I); // or sum = sum + p [I];} printf ("sum = % d", sum );} void sum1 (int a [], int n) {int sum = 0; for (int I = 0; I <n; I ++) {sum = sum + a [I]; // or sum = sum + p [I];} printf ("sum = % d", sum);} int main () {int B [10] = {1, 2, 3, 4, 6, 7, 9, 5, 2, 11}; sum1 (B, sizeof (B)/sizeof (int); return 0 ;}

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.