Home
menubar
rss_feed

Previous Reference C++ Reference <cstddef> Next Reference

C++ Reference: offsetof()

offsetof()


Definition

#define offsetof(s,m) (size_t)&(((s *)0)->m)

Description

Although offsetof looks like a function it is actually just a macro. The result of the macro is a size_t that tells the number of bytes that the member "m" is offset from the beginning of an instance of the data type "s."

Example

#include <iostream>
#include <cstddef>

struct SSomeData {
    char    mcChar;
    int     miInt;
    double  mdDouble;
    bool    mbBool;
};

int main() {
    using namespace std;

    cout << "Offset of mcChar = "
<< offsetof(SSomeData, mcChar) << endl;
    cout << "Offset of miInt = "
<< offsetof(SSomeData, miInt) << endl;
    cout << "Offset of mdDouble = "
<< offsetof(SSomeData, mdDouble) << endl;
    cout << "Offset of mbBool = "
<< offsetof(SSomeData, mbBool) << endl;
    
    return 0;
}


Output:






Previous Reference C++ Reference <cstddef> Next Reference




Home | Reference | Play Games! | Blog | Forum | Site Map | Contact Us


shadow bottom image