结构名称作为 sizeof 的操作数

Name of structure as the operand of the sizeof

当您定义如下结构时

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

那么 sizeof(A) 有效,但 sizeof(num) 无效。谁能解释一下 sizeof 运算符不接受 num 的原因?

因为 num 本身不构成 C 中的 type(只有 struct num 或其 typedef A 构成 类型)。因此,sizeof 失败,因为它的操作数不是它定义的任何一种形式:

sizeof unary-expression
sizeof ( type-name ) 

num 既不是 类型 也不是 一元表达式 .

所以,sizeof(struct num)sizeof(A) 可以,但 sizeof(num) 不行。