sizeof single struct member in C

2020-01-27 09:18发布

I am trying to declare a struct that is dependent upon another struct. I want to use sizeof to be safe/pedantic.

typedef struct _parent
{
  float calc ;
  char text[255] ;
  int used ;
} parent_t ;

Now I want to declare a struct child_t that has the same size as 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_t and struct _parent, but my compiler will not accept.

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_t without the use of dummy?

标签: c struct sizeof
9条回答
兄弟一词,经得起流年.
2楼-- · 2020-01-27 09:56

Although defining the buffer size with a #define is one idiomatic way to do it, another would be to use a macro like this:

#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.

查看更多
在下西门庆
3楼-- · 2020-01-27 09:59

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: I like the member_size macro Joey suggested using this technique, I think I would use that.

查看更多
狗以群分
4楼-- · 2020-01-27 10:01

@joey-adams, thank you! I was searching the same thing, but for non char array and it works perfectly fine even this way:

#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.

And, @brandon-horsley, this works good too:

#define member_dim(type, member) sizeof(((type){0}).member) / \
                                 sizeof(((type){0}).member[0])
查看更多
我想做一个坏孩纸
5楼-- · 2020-01-27 10:04

Use a preprocessor directive, i.e. #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 ;
查看更多
相关推荐>>
6楼-- · 2020-01-27 10:04

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.

查看更多
闹够了就滚
7楼-- · 2020-01-27 10:05

You are free to use FIELD_SIZEOF(t, f) in the Linux kernel. It's just defined as following:

#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.

查看更多
登录 后发表回答