A discussion post on Array pointers in the csdn Forum

Source: Internet
Author: User

This is a good question. I sorted it out for details:

Http://topic.csdn.net/u/20091214/10/da5c0155-6055-4ef5-8cb6-cad8fc72136c.html? Seed = 1804958680 & R = 61913848 # r_61913848

 

Problem:

# Include "stdio. H"
Void main ()
{
Int y [4] [3] = {1, 1}, {1, 1}, {1, 1}, {1, 1 }};
Printf ("% d/N", * (Y + 2) + 2-* y, & Y [2] [2]-y [0], sizeof (* (Y + 2) + 2), sizeof (* y ));
Int Z [4] [3];
Printf ("% d", * (Z + 2) + 2-* z, & Z [2] [2]-Z [0]);
}

The VC compiler is used. An integer occupies 4 bytes and the output result is unknown.
8 press any key
Continue) the first two 8 values are equivalent. In this case, the integer value occupies 4 bytes, which should be 32 rather than 8. Why is it 8? The following results are 4 and 12, I don't know how to understand it,
They all indicate the length of the address. The address in VC is 4 bytes and should be equal. Why is the result different?

 

 

 

Answer:

The Y type is int [4] [3].
In the expression y involved, the value of Y is converted to the INT (*) [3] type.
Y [I] I =, the type of 2 and 3 is int [3],
In the expression y [I], the value of Y [I] is converted to the int * type.

The expression * (Y + 2) + 2-* Y is equivalent to Y [2] + 2-y [0].
The types of Y [2] and Y [0] are converted to int *, and there are 6 int values between them. Therefore, Y [2]-y [0] = 6, 2 is 8.

& Y [2] [2]-y [0]
The Y [2] [2] type is int, And the & Y [2] [2] type is int *, which is 8 different from Y [0], so it is 8

Sizeof (* (Y + 2) + 2) is equivalent to the type of sizeof (Y [2] + 2) y [2] in the formula instead of int *, so the type of Y [2] + 2 is int *, which is 4

In sizeof (* y) * y, convert it to int (*) [3], so * Y is int [3]. Therefore, sizeof (* Y) is 12.
In this case, * Y is Y [0] and the type is int [3]. Therefore, the result is 12.

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.