得出结构体某成员的大小

from http://topic.csdn.net/u/20100416/09/7ff66ea2-dbb6-4515-885d-39498a49eb14.html

#include <stdio.h> #include <stddef.h> typedef struct book { char name[50]; char author[20]; char publish[30]; char isbn[20]; char local[20]; int amount; float price; struct book *next; } BOOK; #define PR_SIZEOF(_t, _m) \ (void)printf("sizeof(%s)=%d\n", #_t "." #_m, (int)sizeof(((_t *)0)->_m)) #define PR_OFFSET(_t, _m) \ (void)printf("offset(%s)=%d\n", #_t "." #_m, (int)offsetof(_t, _m)) int main(void) { PR_SIZEOF(BOOK, name); PR_SIZEOF(BOOK, author); PR_SIZEOF(BOOK, publish); PR_SIZEOF(BOOK, isbn); PR_SIZEOF(BOOK, local); PR_SIZEOF(BOOK, amount); PR_SIZEOF(BOOK, price); PR_SIZEOF(BOOK, next); PR_OFFSET(BOOK, name); PR_OFFSET(BOOK, author); PR_OFFSET(BOOK, publish); PR_OFFSET(BOOK, isbn); PR_OFFSET(BOOK, local); PR_OFFSET(BOOK, amount); PR_OFFSET(BOOK, price); PR_OFFSET(BOOK, next); return (0); } /* output */ /* sizeof(BOOK.name)=50 sizeof(BOOK.author)=20 sizeof(BOOK.publish)=30 sizeof(BOOK.isbn)=20 sizeof(BOOK.local)=20 sizeof(BOOK.amount)=4 sizeof(BOOK.price)=4 sizeof(BOOK.next)=4 offset(BOOK.name)=0 offset(BOOK.author)=50 offset(BOOK.publish)=70 offset(BOOK.isbn)=100 offset(BOOK.local)=120 offset(BOOK.amount)=140 offset(BOOK.price)=144 offset(BOOK.next)=148 */

posted on 2012-07-03 11:04  Orz..  阅读(263)  评论(0)    收藏  举报

导航