Pointer and array in C/C ++ (2)

Source: Internet
Author: User

 

We have discussed some differences between pointers and arrays. However, in some cases, pointers and arrays are equivalent. Next we will discuss when pointers and arrays are the same.

C language standards are described as follows:

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

Note: exceptions in the following situations

1) array name as the operand of sizeof

2) use the & retrieve array address

3) The array is the initial value of A String constant.

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

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

The combination of rule 1 and rule 2 means that the reference to the array subject can always be written as "a pointer pointing to the starting address of the array plus an offset ". For example, a [I] is always parsed as * (a + I) by the compiler.

 

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 correctly compiled and executed. In the expression, expression a is parsed as a pointer to the first element of the array, so the types on both sides of the value assignment symbol match, so you can compile and execute correctly.

Rule 2: The subscript is always the same as the pointer offset. In C, the subscript of the array is rewritten to the pointer offset mainly because the pointer and offset are the basic types used by the underlying hardware. For example, I in a [I] is always parsed as an offset by the compiler, so a [I] is always rewritten into the form of * (a + I, a is the pointer pointing to the first element of the array. The offset I is used to move the pointer to the next step and then take the content of the unit where a + I is located. This explains why the subscript of the array in C can be negative, and in my opinion, the C language does not check whether the subscript of the array is out of the range, as shown in the following program:

# Include <stdio. h>

 

Int main (void)

{

Int a [3] = {1, 2, 3 };

Int * p = (a + 3 );

Printf ("% d \ n", p [-1]);

Return 0;

}

The execution result of the program is 3. Although the subscript is-1, it is parsed as an offset by the compiler, so it is equivalent to P-1 ).

Rule 3: In the declaration of function parameters, the array name is treated as a pointer to the first element of the array by the compiler. In C language, it is out of efficiency considerations to equate the arrays and pointers of form parameters. If this is not done, the values of each element in the entire array are copied and transmitted, so that the overhead in both time and space may be very large. However, to operate on elements in the array, you only need to pass the address of the first element of the array to the calling function, and then access the space to be accessed through the pointer. This will greatly reduce the space-time consumption. Therefore, in a function, the compiler always treats the array name declared in the parameter as a pointer to the first element of the array. In this way, the compiler can generate the correct code, no need to distinguish between arrays and pointers. Therefore, void fun (int a []); and void fun (int * a) have the same effect. If a is referenced in the function, always considered as a pointer by the compiler. Because void fun (int a []); this form will eventually be parsed by the compiler as void fun (int * ); this method tells us that a pointer to the integer data must be passed during the call. Therefore, the following code can be correctly compiled and executed:

# Include <stdio. h>

 

Void fun (int a [])

{

Printf ("% d \ n", a [0]);

}

Int main (void)

{

Int a [3] = {1, 2, 3 };

Int * p1, * p2;

Int B = 4;

P1 =;

P2 = & B;

Fun ();

Fun (& a [1]);

Fun (p1 );

Fun (p2 );

Fun (& B );

Return 0;

}

Differentiate the meanings of several expressions:

& P, p, a, &

& P: the address of the memory unit that obtains the storage pointer Variable p; sizeof (& p) = 4;

P: the address where the pointer Variable p is stored; sizeof (p) = 4;

A: The address of the first element of the array; sizeof (a) = 3*4 = 12;

& A: indicates the first address of the entire array. sizeof (& a) = 4 (in VC ++ 6.0, this value is 12. I think it is incorrect, because its type is array pointer)

Although the values of a and & a are the same, their meanings are completely different. a indicates the address of the first element of the array, and & a indicates the first address of the array. The types they represent are also completely different. a is an int pointer, and & a is an int (* p) [] pointer, that is, the array pointer (which will be explained in subsequent articles ). Therefore, the results of a + 1 and & a + 1 are different, a + 1 indicates moving the pointer to the first element of the array to the back of a step (the step here is the number of bytes occupied by the array element type ); & a + 1 indicates moving the pointer pointing to the array backward by a step (and the step here is the number of array elements * The number of bytes occupied by the element type ).

# Include <stdio. h>

 

Int main (void)

{

Int a [3] = {1, 2, 3 };

Int * p =;

Printf ("% 08x \ n", & p );

Printf ("% 08x \ n", p );

Printf ("% 08x \ n", & p + 1 );

Printf ("% 08x \ n", p + 1 );

Printf ("% 08x \ n", );

Printf ("% 08x \ n", & );

Printf ("% 08x \ n", a + 1 );

Printf ("% 08x \ n", & a + 1); // pay attention to the output result

Return 0;

}

Author: Haizi

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.