箭头运算符位置C

使用此示例结构:

typedef struct
{
    uint8_t ary[2];
    uint32_t val;
}test_t;

这两个代码片段在功能上是等效的:

Snip 1(括号内的箭头操作):

int main()
{
    printf("Hello World");
    printf("n %d", sizeof(((test_t *)0)->ary));

    return 0;
}

Snip 2(括号外的箭头操作):

int main()
{
    printf("Hello World");
    printf("n %d", sizeof((test_t *)0)->ary);

    return 0;
}

如果 ary 的大小发生变化,两者都会报告正确的值,但是我不明白外部括号示例如何工作:

sizeof((test_t *)0) // Using memory address 0 as pointer to test_t. size of pointer should be 4 on 32 bit system

因此,snip 2 中的外部箭头操作应等效于:

4->ary or *4.ary

那么为什么 snip 2 的编译和运行与 snip 1 相同

Expected output: 2

回答

引自N1570 6.5.3 一元运算符:

Syntax
1   unary-expression:
    postfix-expression
        ++ unary-expression
        -- unary-expression
        unary-operator cast-expression
        sizeof unary-expression
        sizeof ( type-name )
        _Alignof ( type-name )

如您所见,sizeof采用表达式的运算符不需要括号。

因此,sizeof(((test_t *)0)->ary)sizeof((test_t *)0)->ary都是有效的。

请注意,这printf("n %d", sizeof(((test_t *)0)->ary));是无效的(调用未定义行为),因为使用了错误的格式说明符。%d用于打印intsizeof运算符返回size_t,其格式说明符是%zu.


以上是箭头运算符位置C的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>