C-array and pointer

Source: Internet
Author: User

Summarize the relationship between arrays and pointers in some C languages.

1. "[]" operation. The subscript operation "[]" is actually defined by another operation. C language specifies the following two expressions: 1 [expression 2] and (* (expression 1) + (expression 2 )))

Is completely equivalent. Because "+" is interchangeable, expression 1 [expression 2] is equivalent to expression 2 [expression 1], for example, int I [1] = 7; I [0] = 0 [I] = 7.

2. the array name is a pointer pointing to the first element of the array, which is a constant. Double D [6] = {2}; "d [0]" equivalent to "(* (d) + (0 )))", it is equivalent to "* D", while "*" is used as a pointer when the unary operator is used. Therefore, "D" is a pointer of the "int *" type, and the array name is a pointer.

3. the array name is not only a pointer, but also the memory occupied by the array.



4. pointer to the array. For arrays, the array name can also represent the memory occupied by the array, so the array pointer can also be obtained by the array name. Int A [20];

"A" and "& A" are both pointer constants with the same value but different data types. "A" is "int *" type pointer. Adding 1 means adding "sizeof (INT)" to the value )", "& A" points to an array like "int [20]", so adding 1 means adding "sizeof (INT [20])".

5. The parameter corresponding to the array name. When an array name is used as a real parameter, the data type corresponding to the form parameter can be described in an incomplete manner, which is actually a pointer type. When int A [10] And a are real parameters, the parameter type should be int [] or int *.

6. The high-dimensional array name is a pointer. For example, int A [2] [3]; A is a pointer to a one-dimensional array composed of three int types (INT (*) [3]), that is, a [0]. Note that, unlike int * [3], int * [3] defines an array. The array element is an int pointer. Note that the result of adding 1 to A and A is different, similar to a one-dimensional array.

7. The high-dimensional array name is the memory occupied by the two-dimensional array.


References: required for entry to C-programmers (about pointers)

C-array and pointer

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.