c - C 编译器如何解释这个程序?

标签 c linux

我有以下程序

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);
}
}

当我运行它时,它给了我以下错误

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

当我按以下方式更改代码时,它工作正常。

pOBJ->pid_test = 1;

我不知道编译器如何解释上面提到的代码。 任何帮助将不胜感激

最佳答案

当您定义了 pid_test 宏时,

pOBJ->_un._testing2.pid_test

扩展为

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

我相信你能发现问题。

关于c - C 编译器如何解释这个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33018514/

相关文章:

linux - tensorflow 和 openSUSE

c++ - 检查信号处理程序是否不是默认的

c++ - 如何诊断 Windows 上的堆损坏错误?

c++ - 在 opencv 中重用设备

c - 提高 C 代码的性能

c - 在 Eclipse for C 中包含外部 header

C 程序 - 输出显示在下一行而不使用换行符

C - 堆上的缓冲区在 EXEC 后不会丢失,而是保存在堆栈上

mysql - 将 linux 用户添加到 `mysql` 组是否安全?

linux - 在由未知字符分隔的同一行中多次匹配和替换一个模式