简体   繁体   English

C编译器如何解释该程序?

[英]How C compiler interpret this program?

I have following program 我有以下程序

struct test
{
int length;
union
{
    struct
    {
        int pid_test;
        int age;
    }_testing1;

    struct
    {
        int pid_test;
        int age;
    }_testing2;
}_un;
};

#define pid_test _un._testing1.pid_test
int main()
{
{
    struct test *pOBJ = (struct test *)malloc( sizeof(struct test) );
    pOBJ->_un._testing2.pid_test = 1;
    free(pOBJ);
}
}

When I run it, it gives me following error 运行它时,出现以下错误

error: ‘struct <anonymous>’ has no member named ‘_un’

When I changed the code in the following way, it works fine. 当我以以下方式更改代码时,它可以正常工作。

pOBJ->pid_test = 1;

I don't know how compiler interpret the above mentioned code. 我不知道编译器如何解释上述代码。 Any help would be highly appreciated 任何帮助将不胜感激

When you have that pid_test macro defined, 定义了pid_test宏后,

pOBJ->_un._testing2.pid_test

expands to 扩展到

pOBJ->_un._testing2._un._testing1.pid_test

I'm sure you can spot the problem. 我确定您可以找到问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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