Understanding of array names

Source: Internet
Author: User
Understanding of array names
Let's take a look at the following code. What results will the program output?
# Include <stdio. h>

Int main ()
{
Int A [5] = {1, 2, 3, 4, 5 };
Int * P = (int *) (& A + 1 );

Printf ("% d \ n", * (a + 1), * (p-1 ));

Return 0;
}
For the answer, see the end of this article.
First, let's analyze the array name step by step. First, let's look at the following code:
# Include <stdio. h>

Int main ()
{
Int A [5] = {1, 2, 3, 4, 5 };
Int * P;

P =;

Return 0;
}
There are no errors in compilation and running. It indicates that the type of a value can be directly assigned to P. At least this indicates that the type of A is one of the types that can be directly assigned to P, that is, a should be a pointer type variable, and the pointer type variable is definitely not int * type, because in the above program, if we output sizeof (A) and sizeof (P) the output value is 20 and the output value is 4, which proves that the two types are different and the program is slightly modified:
# Include <stdio. h>

Int main ()
{
Int A [5] = {1, 2, 3, 4, 5 };
Int * P;

A = P;

Return 0;
}
The compiler reports an error: cannot convert from 'int * 'to 'int [5]'. However, this int [5] does not have this type in C language, this type is obviously the rest after int A [5] removes. It can be understood for the time being that a represents the total number of 20 bytes allocated, and the value of a itself is the starting address of the 20 bytes. Therefore, a has two meanings. Modify the program:
# Include <stdio. h>

Int main ()
{
Int A [5] = {1, 2, 3, 4, 5 };
Int * P;

A ++;

Return 0;
}
The compiler reports the following error: '+ +' needs L-value. This error indicates that ++ requires an operation object that can be used as the left value, and ++ only needs to operate on variables, however, according to the above analysis, A is allocated a 20-Byte variable, and the value of a is the first address of the 20-Byte variable, however, it is not possible to perform the ++ operation on a, which is consistent with the variable modified by const. The variable modified by const is equivalent to being assigned a read-only attribute, that is, the read operation is acceptable and the write operation is invalid. Therefore, the variable modified by const cannot be left-valued, that is, it cannot be re-assigned. Modify the example again:
# Include <stdio. h>

Int main ()
{
Int A [5] = {1, 2, 3, 4, 5 };
Int * P;

P = &;

Return 0;
}
The compiler reports the following error: cannot convert from 'int (*) [5] 'to 'int *'. With this error, the problem becomes clearer and clearer, the Type obtained by the address fetch operation on a is int (*) [5], that is, an array pointer, that is to say, in this case, the understanding of a in the address fetch operation is a 20-byte whole (five int-type units ). However, when P = A is used, the second method of understanding a is to get the value of A, that is, the starting address of the 20 bytes.
The priority of C language is used to analyze the first question. int * P = (int *) (& A + 1) has a priority higher than +, so it is equivalent to int * P (int *) (& A) + 1). This is analyzed step by step. & A obtains the array pointer of five int-type units, & A + 1 is the result of adding 1 to the pointer. That is to say, the current Pointer Points to the first unit after the 20 bytes of, the address of this unit is forcibly converted to int * type and assigned to P, so P points to the first unit after 20 bytes of A, so * (p-1) the obtained value should be the first unit of P, that is, the value of the last unit in the five units of. Therefore, the initial program output is: 2 5.

Understanding of array names

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.