Sizeof typdef'ed structures

A

Ann O'Nymous

How do I do this:


typedef struct foobar {
int a;
char b[16];
float c;
} my_struct;

#define my_structsize sizeof(struct foobar)



I just want to define my_structsize to be the size of the structure
defined in the typdef (one int+16 chars+1 float)

This is just a syntax issue where I can't seem to come up with the right
combination (tried sizeof(my_struct) and several combos)
 
S

Seebs

How do I do this:
typedef struct foobar {
int a;
char b[16];
float c;
} my_struct;
#define my_structsize sizeof(struct foobar)

Well, you just did it.
I just want to define my_structsize to be the size of the structure
defined in the typdef (one int+16 chars+1 float)

Plus possible padding -- although on most systems I don't think you'd see
any here.
This is just a syntax issue where I can't seem to come up with the right
combination (tried sizeof(my_struct) and several combos)

And you also can't seem to tell us what isn't working about this. I
think it might help if you showed us a failing program which tries to
use the my_structsize define.

e.g., if I use the above, then write:

int main(void) { printf("%d\n", (int) my_structsize); return 0; }

I get a program which prints "24\n", which seems like it's working.

-s
 
B

Ben Pfaff

Ann O'Nymous said:
typedef struct foobar {
int a;
char b[16];
float c;
} my_struct;

#define my_structsize sizeof(struct foobar)



I just want to define my_structsize to be the size of the structure
defined in the typdef (one int+16 chars+1 float)

What you have there will work, although you can drop the
"typedef" and "my_struct" parts.
 
A

Ann O'Nymous

How do I do this:
typedef struct foobar {
int a;
char b[16];
float c;
} my_struct;
#define my_structsize sizeof(struct foobar)

Well, you just did it.

OK, it's a macro that uses "my_structsize" that's blowing up. It
complains that "sizeof(struct foobar)" is not an lvalue but is used in a
context that requires one. I have to investigate the macro.

Another statement accepted it without error, had I noticed that, I
wouldn't have blamed the sizeof().
 
I

Ian Collins

How do I do this:
typedef struct foobar {
int a;
char b[16];
float c;
} my_struct;
#define my_structsize sizeof(struct foobar)

Well, you just did it.

OK, it's a macro that uses "my_structsize" that's blowing up. It
complains that "sizeof(struct foobar)" is not an lvalue but is used in a
context that requires one. I have to investigate the macro.

There's nothing wrong with the macro, you appear to be using it wrong.
I say appear because you haven't posted the offending code, please do!
 
S

Seebs

OK, it's a macro that uses "my_structsize" that's blowing up. It
complains that "sizeof(struct foobar)" is not an lvalue but is used in a
context that requires one. I have to investigate the macro.
Ah-hah!

Another statement accepted it without error, had I noticed that, I
wouldn't have blamed the sizeof().

From this we derive the golden rule of debugging:

Try to build a smaller reproducer. :)

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top