sizeof结构中的相对指针 [英] sizeof a relative pointer in a struct
问题描述
大家好。
我正试图通过他的亲戚获得结构变量的大小
postion即
///
#define offsetof(s,m)(size_t)&(((s *)0) - > m)
struct ThePimp {
char ring [10];
char blings [20];
};
int sizeOfBling = sizeof((* char)& ThePimp +
(char)offsetof(ThePimp,blings));
///
(如果我有任何语法错误请忽略)
你可以很容易地预测sizeOfBling将是4 ......( sizeof(int *))
有没有办法来实现呢?
谢谢
Hi all.
I''m trying to get the size of a variable in a struct by his relative
postion i.e.
///
#define offsetof(s,m) (size_t)&(((s *)0)->m)
struct ThePimp{
char rings[10];
char blings[20];
};
int sizeOfBling = sizeof( (*char)&ThePimp +
(char)offsetof(ThePimp,blings) );
///
(If I have any syntax mistakes please ignore)
You can easily predict that sizeOfBling will be 4... (sizeof(int*))
Is there any way to acomplish it?
Thanks
推荐答案
ManicQin写道:
ManicQin wrote:
#define offsetof(s,m)(size_t)&(((s *)0) - > m )
#define offsetof(s,m) (size_t)&(((s *)0)->m)
AFAIU,但定义了offsetof,它在stddef.h中定义
AFAIU, however offsetof is defined, it is defined in stddef.h
cat foo.c
#inclu de< stdio.h / * printf * /
#include< stddef.h / * offsetof * /
struct ThePimp {char rings [10]; char blings [20]; };
int main(void)
{
printf("%d \ n",(int)offsetof(struct ThePimp) ,blings));
返回0;
}
cat foo.c
#include <stdio.h /* printf */
#include <stddef.h /* offsetof */
struct ThePimp { char rings[10]; char blings[20]; };
int main(void)
{
printf("%d\n", (int)offsetof(struct ThePimp, blings));
return 0;
}
gcc -std = c89 - 迂腐-Wall -Wextra foo.c
gcc -std=c89 -pedantic -Wall -Wextra foo.c
这篇关于sizeof结构中的相对指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!