[C/C ++] evaluate the struct member's offset address

Source: Internet
Author: User

For example
Typedef struct struc {
Int;
Char B [20];
Double ccc;
};

The offset of B to struc is required.

 


In fact, we have long known that there are (size_t) & (struc *) 0)-> B) such statements. Yesterday, my roommate saw this "weird" style in the "interview book" on the third edition and asked me what was going on.

 


I suddenly came up with the idea of "using the Member address-structure address. I tried it.

 


Printf ("% d", (struc *) & (stc. B)-& stc); // actually outputs 0 !?

 


However, the difference between (struc *) & (stc. B) and & stc is indeed 4 bytes. // Because "." has a higher priority than "&", & (stc. B) is & stc. B.

 


Think twice about it. Ask questions in the Forum. The answer is: "The result of two pointers Subtraction is the offset of the two pointers. This offset is relative to the pointer type. For example, if two int-type pointers subtract from each other to get a value of 1, it does not mean that the difference between the two addresses is one byte. This is also impossible because the int type is generally four bytes. So this 1 refers to the difference of the size of 1 int (4 bytes ). Similarly, (struc *) & (stc. B)-& stc), the difference value is less than a struc type, and it is actually 0. If you want to see the address difference, convert them into numerical values: (intptr_t) & (stc. B)-(intptr_t) & stc ." (Original post see http://forum.byr.edu.cn/article/CPP/53484)

 


Suddenly realized!

Changed

 


Printf ("% d", (stc. B)-(char *) & stc); // OK!

 

Printf ("% d", (char *) (& stc. b)-(char *) & stc); // This method may be more accurate if it is a byte.

 


I tried it again.

 


Printf ("% d", (bool *) (& stc. B)-(bool *) & stc ));

 


Is it true ?!

 


Later, another speaker said: "offsetof has always been part of the C language standard library. Use it directly ."

 


Standard library ?! Surprised. Read it

 


Qiu zongyan. From problem to program: programming and C language introduction. Beijing: Machinery Industry Press, 2005.9, P372

 


The book says: "The file <stddef. h> contains some common definitions of the standard library. No matter which standard header file we include, <stddef. h> will be automatically included. Definition in this file:... macro offsetor ......"

 


Offsetor ?! Is it a printing error? I even found a blog that made the same mistake! See html> http://hi.baidu.com/esta_pessoa/blog/item/ffc91b3f225e95e755e72341.html

 


It seems that it is an error, because my VS2008 does not have offsetor, and only has offsetof. Also, <stddef. h> must be explicitly included, at least as required by VS2008. All right, give it a try,

 


Printf ("% d", offsetof (stc, stc. B); // Error!

 


Printf ("% d", offsetof (stc, B); // or Error !!!

 


Speechless.

 


Check C ++ Reference (http://www.cplusplus.com/reference), actually want this

 


Printf ("% d", offsetof (struc, B ));//!!!

 

 

Although there are still a lot of questions I don't understand, it's time to find the offset address.

 


Bytes ------------------------------------------------------------------------------------------------


To sum up,

 


For a struct, such

 


Typedef struct struc {
Int;
Char B [20];
Double ccc;
};

 


The offset of a member, such as B, relative to struc.

 


You can:

 


// Method 1: Step on the step of the predecessors

(Size_t) & (struc *) 0)-> B)

 


// Method 2: standing on the shoulders of giants

Offsetof (struc, B) // NOTE: to explicitly include the header file <stddef. h>

 


// Method 3

(Size_t) (char *) (& stc. B)-(char *) & stc)

 

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.