简体   繁体   中英

Name of structure as the operand of the sizeof

When you define a structure like below

typedef struct num {
   int a; 
   int b; 
} A;  

Then sizeof(A) is valid, but sizeof(num) isn't. Could anyone please explain the reason why the sizeof operator doesn't accept num ?

Because num on its own doesn't constitute a type in C (only struct num or its typedef A constitutes the type ). So, sizeof fails as its operand is not in either form of that it's defined for:

sizeof unary-expression
sizeof ( type-name ) 

ie num is neither a type nor a unary-expression .

So, sizeof(struct num) or sizeof(A) is fine but sizeof(num) isn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM