Description of offset macro

Source: Internet
Author: User
1. Macro description of offset

# Define offsetof (type, member) (size_t) & (type *) 0)-> Member)

The macro can be explained in the following four steps:

1> (type *) 0) 0 address forced "Conversion" to type structure type pointer;

2> (type *) 0)-> member accesses the member data member in the type structure;

3> & (type *) 0)-> Member: extracts the address of the data member in the type structure;

4> (size_t) (& (type *) 0)-> member) The result is converted to the size_t type.

The clever feature of macro offsetof is to forcibly convert the 0 address to a pointer of the type structure type. If the type structure uses the first address 0 of the memory space as the starting address, the Member Address is naturally an offset address. Some readers may wonder if they have to use 0? Of course not. We just want to make the computation simple. It can also be used as its value, except that the calculated result minus this value is the offset address. Let's take a look at the following code:

# Include <stdio. h>

# Defineoffsetof (type, member) (size_t) & (type *) 4)-> Member)

Struct test_struct {

Int num;

Char ch;

Float F1;

};

Int main (void)

{

Printf ("offsetof (struct test_struct, num) = % d \ n", offsetof (struct test_struct, num)-4 );

Printf ("offsetof (structtest_struct, CH) = % d \ n", offsetof (struct test_struct, CH)-4 );

Printf ("offsetof (struct test_struct, F1) = % d \ n", offsetof (struct test_struct, F1)-4 );

Return 0;

}

The running result is:

Jibo @ Jibo-virtualbox :~ /Cv_work/work/LIST/Offset $./main

Offsetof (struct test_struct, num) = 0

Offsetof (struct test_struct, CH) = 4

Offsetof (struct test_struct, F1) = 8

To impress everyone, we didn't use 0 in the code, but used 4, so the offset address is subtracted from the final result, of course, we use 0 in actual use.

2. Use the offsetof macro as an example:

# Include <stdio. h>

# Defineoffsetof (type, member) (size_t) & (type *) 0)-> Member)

Struct test_struct {

Int num;

Char ch;

Float F1;

};

Int main (void)

{

Printf ("offsetof (struct test_struct, num) = % d \ n", offsetof (struct test_struct, num ));

Printf ("offsetof (structtest_struct, CH) = % d \ n", offsetof (struct test_struct, CH ));

Printf ("offsetof (struct test_struct, F1) = % d \ n", offsetof (struct test_struct, F1 ));

Return 0;

}

The execution result is:

Jibo @ Jibo-virtualbox :~ /Cv_work/work/LIST/Offset $./main

Offsetof (struct test_struct, num) = 0

Offsetof (struct test_struct, CH) = 4

Offsetof (struct test_struct, F1) = 8

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.