101 when will the array be converted to a pointer

Source: Internet
Author: User

C Learning Network: Link->C Learning Network

C Training Network: Link->C Training Network An array name is meant to represent a set of data that, like a normal variable, is used to refer to a piece of memory, but in use, the array name is sometimes converted to a pointer (address) to a data set, rather than a collection of data itself. This has been confirmed several times in the previous example.

The data set contains multiple data, the direct use of a set does not have a clear meaning, the array name to a pointer to the array, you can easily access any of these data, the use of more explicit semantics.

The C language standard stipulates that when an array name is an array-defined identifier (that is, when the arrays are defined or declared), sizeof, or & operands, it represents the entire array itself, and in other expressions the array names are converted to pointers (addresses) to the No. 0 element.

The relation between the array and the pointer is quite like the relationship between poetry and word, they are all a kind of literary form and have many similarities, but they have their own characteristics in the actual expressive methods.
on the array subscript [] The C language standard also stipulates that the array subscript is the same as the offset of the pointer. In layman's sense, a reference to an array subscript can always be written as "a pointer to the starting address of the set plus an offset." Suppose you now have an array A and a pointer variable p, which is defined in the form:

int a = {1, 2, 3, 4, 5}, *p, i = 2; Readers can access a[i in any of the following ways:

p = A;
P[i];

p = A;
* (P + i);

p = a + i;
*p;

A reference to an array a[i] is always compiled with a compiler that rewrites the form of * (A+i), and the C language standard requires that the compiler must have this behavior.

The remove Mark operator [] is based on the pointer, it does this by adding a pointer to an integer, producing a new pointer, and then getting the data from the new pointer (the new address), and assuming the type of the pointer is T *, the type of result is T.

The two operands of the removal mark operator are interchangeable, and it does not care about the order of the operands, as in addition 3+5 and 5+3 are nothing different. Take the array a above as an example, if you want to access the 3rd element, then you can write A[3], you can write 3[a], both forms are correct, but the form has never been used, except that it can make beginners dizzy, there is no practical significance. A[3] is equivalent to * (A + 3), and 3[a] is equivalent to * (3 + a), merely swapping the two operands of the addition. When you use subscript, the compiler automatically adjusts the step of the subscript to the size of the array element. Each element in array A is of type int with a length of 4 bytes, then a[i+1] and A[i are 4 (instead of 1) in memory. array as a function parameterThe C language standard stipulates that the formal parameter as an array of types should be adjusted to "pointer of type". In the case of a function parameter definition, the compiler must overwrite the form of the array as a pointer to the NO. 0 element of the array exponentially. The compiler only passes the address of an array to a function, not a copy of the entire array.

This implicit conversion means that the following three types of function definitions are completely equivalent:
void func (int *parr) {.....} void func (int arr[]) {...} void func (int arr[5]) {...} within a function, arr is converted to a pointer variable, the compiler is a The RR allocates 4 bytes of memory, and sizeof (ARR) evaluates the length of the pointer variable, not the length of the array. To get the length of an array within a function, you must add an extra parameter to the length of the array before calling the function, which is highlighted in the section "C language pointer variables as function parameters."

Parameter passing is the process of assigning a value, which is also an expression, the effect is the same regardless of whether an array name or array pointer is passed, which is equivalent to assigning a pointer variable.

Equating arrays and pointers as parameters is considered for efficiency reasons. An array is a collection of data of the same type, with no limit to the number of data, perhaps only a few, or thousands, that can be very costly to pass through the entire array, both in time and in memory space. And most of the time, we don't really need a copy of the entire array, we just want to tell the function which is interested in which particular array at that moment. a summary of the commutative nature of arrays and pointers1) access to arrays in the form of a[i] will always be rewritten by the compiler (or interpreted as) as a pointer form like * (A+i).

2 The pointer is always a pointer, and it must never be rewritten as an array. You can use the subscript form to access the pointer, which is usually the pointer as a function parameter, and you know that the actual pass to the function is an array.

3 in a particular environment, that is, an array as a function parameter, and only in this case, an array can be seen as a pointer. An array that is a function parameter is always modified by the compiler to exponentially a pointer to the first element of the array.

3 When you want to pass an array to a function, you can define the function arguments as arrays (you can specify a length or no length), or you can define a pointer. Either way, it is treated as a pointer variable inside the function.

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.