Skip to content

Instantly share code, notes, and snippets.

@Keith-S-Thompson
Created February 7, 2012 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keith-S-Thompson/1756545 to your computer and use it in GitHub Desktop.
Save Keith-S-Thompson/1756545 to your computer and use it in GitHub Desktop.
offsetof() example
/*
* 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;
}
// 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