C: sizeof single struct member

I am trying to declare a structure that depends on a different structure. I want to use sizeof in order 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 is 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 several different ways with parent_t and struct _parent , but my compiler does 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 using dummy ?

+73
c struct sizeof
Aug 24 2018-10-10T00:
source share
9 answers

Although defining a buffer size using #define is one idiomatic way to do this, another will use a macro like this:

 #define member_size(type, member) sizeof(((type *)0)->member) 

and use it as follows:

 typedef struct { float calc; char text[255]; int used; } Parent; typedef struct { char flag; char text[member_size(Parent, text)]; int used; } Child; 

Actually, I'm a little surprised that sizeof((type *)0)->member) even allowed as a constant expression. Cool stuff.

+126
Aug 24 '10 at 3:13
source share

I'm 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) 

<h / "> Edit . I like the member_size macro. Joey suggested using this technique, I think I will use this.

+19
Aug 24 '10 at 3:12
source share

Use the 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 ; 
+9
Aug 24 '10 at 3:10
source share

You can use the preprocessor directive for size like:

 #define TEXT_MAX_SIZE 255 

and use it in both parent and child.

+4
Aug 24 '10 at 3:10
source share

Another possibility is to determine the type. I think that the fact that you want to provide the same size for two fields is an indicator that you have the same semantics for them.

 typedef char description[255]; 

and then the field

 description text; 

in both of your types.

+4
Aug 24 '10 at 5:47
source share

struct.h already defined,

 #define fldsiz(name, field) \ (sizeof(((struct name *)0)->field)) 

so you can

 #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, looking in the title, it seems that this is a BSD thing, not an ANSI or POSIX standard. I tried this on a Linux machine and it did not work; limited utility.

+3
Jun 13 '16 at 22:25
source share

C ++ solution:

sizeof (Type :: member) also works:

 struct Parent { float calc; char text[255]; int used; }; struct Child { char flag; char text[sizeof(Parent::text)]; int used; }; 
+2
Feb 26 '16 at 12:47
source share

You can use sizeof_field(type, filed) on Linux. It is defined only as follows:

 #define sizeof_field(type,field) (sizeof(((type *)0)->field)) 

mentioned above. But it is more portable to use an already defined macro.

+1
Mar 13 '17 at 5:27
source share

@ joey-adams, thanks! I was looking for the same thing, but for a char array, and it works just 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 well too:

 #define member_dim(type, member) sizeof(((type){0}).member) / \ sizeof(((type){0}).member[0]) 
0
Sep 30 '17 at 9:12
source share



All Articles