Interesting things in C Language _ pointer Conversion

Source: Internet
Author: User

Today, I saw this post in the group discussion post of Gmail:

[Learn-Linux-c- CPP] converting a one-dimensional array to a pointer to an array inbox x zhangzhe benzhemin@gmail.com  11 - 10 - 28  Send to learning - Linux:Code: # Include <Stdio. h> Int Main ( Void  ){  Int A [ 10 ] = { 1 ,2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10  };  Int (* P )[ 2 ] = ( Int (*)[ 2 ]) A; printf (  "  % D \ n  " ,** P); printf (  "  % D \ n  " , ** (P + 1  ); Printf (  "  % D \ n  " ,* A );  Return   0 ;} A is just a pointer, why is it converted to int ( *)[ 2  ] Is it converted to a two-dimensional pointer? In fact, the following code can also be compiled and run. # Include <Stdio. h> Int Main ( Void  ){  Int A [ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10  };  Int (* P )[ 2 ] = ( Int (*)[ 2 ]) & A; printf (  "  % D \ n  " ,** P); printf (  " % D \ n  " , ** (P + 1  ); Printf (  "  % D \ n  " ,* A );  Return   0  ;} Hard  Int (* P )[ 2 ] = ( Int (*)[ 2 ]) A; and Int (* P )[ 2 ] = ( Int (*)[ 2 ]) And a; are the two conversions equivalent.

Here we can clearly see that this netizen is confused with the concept, as described below.

 
A is just a pointer. Why is it converted to int (*)[2] Is it converted to a two-dimensional pointer?

We know that in C language, the array name is a constant, that is, the const volume. After the above changes, A is not converted to a pointer type such as int (*) [2, it is still maintained

The original property, except that p is a pointer type such as int (*) [2] and is forcibly directed to the address. This means that if the pointer P is used for access,

The 10 integers are interpreted as two-dimensional arrays.

There is also the following description:

HardInt(* P )[2] = (Int(*)[2]) A; andInt(* P )[2] = (Int(*)[2]) And a; are the two conversions equivalent.

The two descriptions above are consistent, because during parsing: the essence of a and & A is the same, and they all point to a fixed area in the memory; therefore, the two conversions are equivalent.

 

I don't want to talk about it here. Welcome the heroes in the garden to discuss this issue.

The above is my personal point of view. If there is any improper situation, please make a picture .........................

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.