Skip to content

Instantly share code, notes, and snippets.

@zlargon
Created January 24, 2017 15:13
Show Gist options
  • Save zlargon/8dcc7a102d7d4fa6dd9b52f73cbfb629 to your computer and use it in GitHub Desktop.
Save zlargon/8dcc7a102d7d4fa6dd9b52f73cbfb629 to your computer and use it in GitHub Desktop.
implement "sizeof"
#include <stdio.h>
typedef struct {
int i;
char a;
char b[3];
float * c;
char s[100];
} T;
#define _sizeof(T) ({ \
typeof(T *) p = 0; \
((size_t) (p + 1)); \
})
int main() {
printf("sizeof int = %zu\n", _sizeof(int)); // 4
printf("sizeof struct T = %zu\n", _sizeof(T)); // 120
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment