Macro definitions related to struct (member variable offset and get struct start address)

Source: Internet
Author: User
Tags printf

People who have learned C language know that we can define our own data collection through struct, so as to realize the encapsulation function of the data.

In practical project practice, the structure of struct definition is ubiquitous. The use of struct is not difficult, there are many articles on the internet to introduce struct.

From the definition of structure to how to use it, speak very carefully. I don't talk much about it here.

This article mainly describes how to get the offset of the member variable in struct and get the starting address of the struct through the address of the member variable.

The second issue is the close reliance on the first question.

1. Gets the offset of the member variable.

Go directly to the code:

#define OFFSET_OF (Type, field) ((unsigned int) & ((type *) (0))->field)

This macro is more difficult for people who are just beginning to learn C language.

To understand this macro, you first need to understand what the macro definition is all about, what the struct is all about, and the use of pointers.

Briefly explain this macro:

If we define a struct:

typedef struct{

int A;

int b;

A

Then offset_of (A, b) outputs the offset of B relative to the starting address.

&-This is the address symbol that takes the address of the current variable.

0-This is the most central part of the macro. We know that for a struct, such as the above A, &a->b represents the address of B, and if we want B to be offset from a, then &a->b-&a can get it.

But if &a is 0. So &a->b is just an excursion.

Okay, when we have a macro that gets the relative offset of the member variable. If the address of a member variable is known, then we can easily get the first address of the member's structure.

The code is as follows:

#define CONTAINER_OF (PTR, type, field) (Type *) ((char *) ptr-offset_of (type, field)
All we need to do is subtract the address of the member variable from your offset from the first address, which is the first address of the structure.

Well, not much to say, to produce a complete example:

TestConsole.cpp:Defines the entry point for the console application.
#include "stdafx.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#define OFFSET_OF (Type, field) (unsigned int) & ((type *) (0)->field))
#define CONTAINER_OF (PTR, type , field) (Type *) ((char *) ptr-offset_of (type, field))

typedef struct{
    int     A;
    int     b;
    Char    C;
    int     D;
} package_t;

int main ()
{
    package_t pkg;
    int *p;

    p = (int *) &pkg.d;
    printf ("Offset of ' d ' is%d.\n", offset_of (package_t, D));
    printf ("The addr of pkg = 0x%x.\n", &pkg);
    printf ("The addr of d ' s container = 0x%x.\n", container_of (p, package_t, D));
    return 0;
}




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.