An elementary introduction to arrays and pointers in C (II.)

Source: Internet
Author: User

Original Reprint Address: http://www.cnblogs.com/dolphin0520/archive/2011/11/09/2242419.html

On the basis of the original text to increase their understanding as a modification

A brief talk on pointers and arrays (II.)

Some of the differences between pointers and arrays have been discussed earlier, but in some cases pointers and arrays are equivalent, and the following discusses when pointers and arrays are the same.

This is illustrated by the C language standard:

Rule 1: The array name in an expression is used by the compiler as a pointer to the first element of the array;

Note: The following cases are exceptions

1) array name as the operand of sizeof

2) Use & to take the address of the array

Rule 2: The subscript is always the same as the pointer offset;

Rule 3: In the declaration of a function parameter, the array name is used by the compiler as a pointer to the first element of the array.

Rule 1 and Rule 2 are combined to understand that a reference to an array's subscript can always be written as "a pointer to the starting address of a set plus an offset". such as A[i] are always parsed by the compiler into the form of * (A+i) .

Rule 1: The array name in the expression is always parsed as a pointer by the compiler, so the following statement, int a[3];int *p=a, can be compiled correctly. In an expression where a is parsed as a pointer to the first element of the array, the type on either side of the assignment symbol matches, so the execution is compiled correctly.

Rule 2: The subscript is always the same as the pointer offset. The main reason for rewriting the subscript of an array as a pointer offset in c is that pointers and offsets are the basic types used by the underlying hardware. As in A[i], I is always resolved to an offset by the compiler, so a[i] is always rewritten as * (A+i), A is a pointer to the first element of the array, plus the offset I, indicating that the pointer moves backward I step, and then take the contents of the cell a+i. This can explain why the subscript in the C language can be negative, and in my opinion, the C language does not check the array subscript is also related to this, such as the following procedure:

#include <stdio.h>int main (void) {    int a[3]={1, 2,3};     int *p= (+3);    printf ("%d\n", p[-1]);     return 0 ;}

The result of the program execution is 3, although the subscript is-1, but the compiler resolves to an offset, so it is equivalent to * (P-1).

Rule 3: In the declaration of a function parameter, the array name is used by the compiler as a pointer to the first element of the array. In C language, an array of parameters is equated with pointers for efficiency reasons. If you do not, the value of each element of the entire array is copied in one copy for delivery, so that the overhead of both time and space can be very large. However, to manipulate the elements in the array, you simply pass the address of the first element of the array to the calling function, and then use the pointer to access the space you want to access, so that the time-space consumption is greatly reduced. So inside the function, the compiler always takes the name of the array declared in the argument as a pointer to the first element of the array, so that the compiler can produce the correct code and does not need to differentiate between the two cases of arrays and pointers. so void fun (int a[]), and void fun (int *a) are exactly the same, and in the inside of a function it is always a pointer to the compiler to refer to a. because void fun (int a[]), this form will eventually be parsed by the compiler to void fun (int *a), which tells us that the call must pass a pointer to the integer data. So the following code can be compiled and executed correctly:

An elementary introduction to arrays and pointers in C (II.)

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.