C Standard Libraries C++



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."


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


#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;


offsetof() Output

© 2007–2024 XoaX.net LLC. All rights reserved.