-
-
Save Keith-S-Thompson/1756545 to your computer and use it in GitHub Desktop.
offsetof() example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Reference: http://stackoverflow.com/questions/9169453/gcc-4-4-3-offsetof-constant-expression-bug-how-should-i-work-around-this | |
* Should compile and execute without error or warning with: | |
* gcc c.c -pedantic -std=c89 -o c && ./c | |
* or | |
* gcc c.c -pedantic -std=c99 -o c && ./c | |
*/ | |
#include <stddef.h> | |
#include <stdlib.h> | |
struct SomeType { | |
int m_member; | |
}; | |
int main(void) { | |
switch (0) { | |
case offsetof(struct SomeType, m_member): | |
exit(EXIT_SUCCESS); | |
default: | |
exit(EXIT_FAILURE); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reference: http://stackoverflow.com/questions/9169453/gcc-4-4-3-offsetof-constant-expression-bug-how-should-i-work-around-this | |
// Should compile and execute without error or warning with: | |
// g++ c.cpp -pedantic -std=c++98 -o c && ./c | |
// or | |
// g++ c.cpp -pedantic -std=c++0x -o c && ./c | |
#include <cstddef> | |
#include <cstdlib> | |
struct SomeType { | |
int m_member; | |
}; | |
int main() { | |
switch (0) { | |
case offsetof(SomeType, m_member): | |
exit(EXIT_SUCCESS); | |
default: | |
exit(EXIT_FAILURE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment