C语言 C 中单个结构成员的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3553296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sizeof single struct member in C
提问by kevinarpe
I am trying to declare a struct that is dependent upon another struct.
I want to use sizeofto be safe/pedantic.
我正在尝试声明一个依赖于另一个结构的结构。我想使用sizeof安全/迂腐。
typedef struct _parent
{
float calc ;
char text[255] ;
int used ;
} parent_t ;
Now I want to declare a struct child_tthat has the same size as parent_t.text.
现在我想声明一个child_t与parent_t.text.
How can I do this? (Pseudo-code below.)
我怎样才能做到这一点?(下面的伪代码。)
typedef struct _child
{
char flag ;
char text[sizeof(parent_t.text)] ;
int used ;
} child_t ;
I tried a few different ways with parent_tand struct _parent, but my compiler will not accept.
我用parent_tand尝试了几种不同的方法struct _parent,但我的编译器不接受。
As a trick, this seems to work:
作为一个技巧,这似乎有效:
parent_t* dummy ;
typedef struct _child
{
char flag ;
char text[sizeof(dummy->text)] ;
int used ;
} child_t ;
Is it possible to declare child_twithout the use of dummy?
是否可以在child_t不使用的情况下进行声明dummy?
回答by Joey Adams
Although defining the buffer size with a #defineis one idiomatic way to do it, another would be to use a macro like this:
虽然用 a 定义缓冲区大小#define是一种惯用的方法,但另一种方法是使用这样的宏:
#define member_size(type, member) sizeof(((type *)0)->member)
and use it like this:
并像这样使用它:
typedef struct
{
float calc;
char text[255];
int used;
} Parent;
typedef struct
{
char flag;
char text[member_size(Parent, text)];
int used;
} Child;
I'm actually a bit surprised that sizeof((type *)0)->member)is even allowed as a constant expression. Cool stuff.
我实际上有点惊讶sizeof((type *)0)->member)甚至允许作为常量表达式。很酷的东西。
回答by Brandon Horsley
I am not on my development machine right now, but I think you can do one of the following:
我现在不在我的开发机器上,但我认为您可以执行以下操作之一:
sizeof(((parent_t *)0)->text)
sizeof(((parent_t){0}).text)
Edit编辑:我喜欢 Joey 建议使用这种技术的 member_size 宏,我想我会使用它。
回答by Dmitry
You are free to use FIELD_SIZEOF(t, f)in the Linux kernel.
It's just defined as following:
您可以FIELD_SIZEOF(t, f)在 Linux 内核中自由使用。它只是定义如下:
#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
This type of macro is mentioned in other answers. But it's more portable to use an already-defined macro.
其他答案中提到了这种类型的宏。但是使用已定义的宏更易于移植。
回答by dave mankoff
Use a preprocessor directive, i.e. #define:
使用预处理器指令,即#define:
#define TEXT_LEN 255
typedef struct _parent
{
float calc ;
char text[TEXT_LEN] ;
int used ;
} parent_t ;
typedef struct _child
{
char flag ;
char text[TEXT_LEN] ;
int used ;
} child_t ;
回答by codaddict
You can use a preprocessor directive for size as:
您可以将预处理器指令用于大小:
#define TEXT_MAX_SIZE 255
and use it in both parent and child.
并在父母和孩子中使用它。
回答by Neil
struct.hhas them already defined,
struct.h他们已经定义了,
#define fldsiz(name, field) \
(sizeof(((struct name *)0)->field))
so you could,
这样你就可以
#include <stdlib.h> /* EXIT_SUCCESS */
#include <stdio.h> /* printf */
#include <struct.h> /* fldsiz */
struct Penguin {
char name[128];
struct Penguin *child[16];
};
static const int name_size = fldsiz(Penguin, name) / sizeof(char);
static const int child_size = fldsiz(Penguin, child) / sizeof(struct Penguin *);
int main(void) {
printf("Penguin.name is %d chars and Penguin.child is %d Penguin *.\n",
name_size, child_size);
return EXIT_SUCCESS;
}
but, on looking in the header, it appears that this is a BSD thing and not ANSI or POSIX standard. I tried it on a Linux machine and it didn't work; limited usefulness.
但是,在查看标题时,这似乎是 BSD 的东西,而不是 ANSI 或 POSIX 标准。我在 Linux 机器上试了一下,没用;用处有限。
回答by Jens Gustedt
Another possibility would be to define a type. The fact that you want to ensure the same size for the two fields is an indicator that you have the same semantics for them, I think.
另一种可能性是定义一个类型。我认为,您希望确保两个字段的大小相同这一事实表明您对它们具有相同的语义。
typedef char description[255];
and then have a field
然后有一个字段
description text;
in both of your types.
在你的两种类型中。
回答by korish
c++ solution:
C++解决方案:
sizeof(Type::member) seems to be working as well:
sizeof(Type::member) 似乎也在工作:
struct Parent
{
float calc;
char text[255];
int used;
};
struct Child
{
char flag;
char text[sizeof(Parent::text)];
int used;
};
回答by Roman Kovtuh
@joey-adams, thank you! I was searching the same thing, but for non char array and it works perfectly fine even this way:
@joey-adams,谢谢!我正在搜索同样的东西,但对于非字符数组,即使这样它也能很好地工作:
#define member_dim(type, member) sizeof(((type*)0)->member) / \
sizeof(((type*)0)->member[0])
struct parent {
int array[20];
};
struct child {
int array[member_dim(struct parent, array)];
};
int main ( void ) {
return member_dim(struct child, array);
}
It returns 20 as expected.
它按预期返回 20。
And, @brandon-horsley, this works good too:
而且,@brandon-horsley,这也很好用:
#define member_dim(type, member) sizeof(((type){0}).member) / \
sizeof(((type){0}).member[0])

