struct EMPLOYEE {...} em1,em2;或struct EMPLOYEE {...};

时间:2013-03-21 01:09:03

标签: c struct

这两个是否相同?

struct EMPLOYEE {
...
};

int main (void)
{
    //Local Declaration
    struct EMPLOYEE em1;
    struct EMPLOYEE em2;
    ...

    return 0;
}

struct EMPLOYEE {
...
}em1,em2;

如果我使用后一种情况,我是不是必须像在前一种情况下那样在主函数中声明这些结构?

2 个答案:

答案 0 :(得分:2)

struct EMPLOYEE {
...
};

定义了一个名为EMPLOYEE的结构,但不会创建任何实例。

struct EMPLOYEE {
...
}em1,em2;

定义结构EMPLOYEE并创建两个名为em1em2的实例。

答案 1 :(得分:0)

使用

struct EMPLOYEE {
...
}em1,em2;

然后你可以按照以下方式使用它们

int main (void)
{
    em1.memberx = 2; /*assuming memberx is a member of your structure.*/
    return 0;
}

否则你必须像在主函数示例中那样声明它们。

您还有另一种选择:

typedef struct
{
...
}EMPLOYEE;

然后将它们声明为:

EMPLOYEE em1;
EMPLOYEE em2;

并在你的主要使用它们:

int main (void)
    {
        em1.memberx = 2; /*assuming memberx is a member of your structure.*/
        return 0;
    }