Super weird C language pointer computing

Source: Internet
Author: User
Int x = (int (*) [7]) 256-(int (*) [7]) 0;
After running, the x result is 9. I don't know how to explain it? # Include <stdio. h>

Typedef int (* p) [7]; // The result here is the same as that in your field, but you are given an empty name type.
Typedef p PA;

Int main ()
{
Int x = (PA) 256-(PA) 0;

Printf ("x = % d/n", x );
Return 0;
}

First of all, you do not understand why your program outputs 9,
To be honest, you just don't really understand the pointer. Let's analyze it first.
(*) [7]: indicates a pointer pointing to an array of 7 elements,
Int...: indicates that these elements are of the int type.
So:
(Int (*) [7]): indicates a pointer to an array of seven int elements.
Then, the subtraction between the (PA) 256 type and the (PA) 0 type is the subtraction between the pointer and the pointer,
The offset between pointer and pointer is related to type. The type here is
(Int (*) [7]) type, which is expressed by PA and easier to understand! Haha!
How big is PA ?? 28? Haha! No, it's 4
PA is a pointer pointing to an array.
It's just the type of PA, but it's another type. Oh, isn't it a bit dizzy !!
So,
(PA) 256 is forced to convert 256 into a pointer pointing to [7,
What is the size of [7? 28 !!! Oh, that's right. The type is int.
Of course !!!
So
256/28-256/0 ====== ??? How much? 9
This is what it looks like!
You can give it a try. Change [7] to 8. Try it. Can you guess it now? I think it should be 8.
Not necessarily. Try it !!! To be honest, I did not understand :(

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.