4

I am just wondering how typedef is not throwing any compilation error when its used with undeclared structure. Below code is compiling without any warning and error. My doubt is how come a typedef with undeclared structure is not throwing any error. Is it same in all platforms

#include <stdio.h>

typedef struct undeclared_struct_st UND_STRUCT_S;

int main()
{
    printf("\nhello world\n");
    return 0;
}

I am executing this program in Suse 11 with gcc 4.3.4.

4

2 回答 2

8
typdef struct undeclared_struct_st UND_STRUCT_S;

已验证。它声明struct undeclared_struct_st为不完整的类型,然后声明UND_STRUCT_Sstruct undeclared_struct_st. 您不能创建不完整类型的对象,但可以创建指向不完整类型对象的指针。struct undeclared_struct_st然后可以在另一个翻译单元中声明。

于 2013-07-22T11:53:15.583 回答
5

这称为前向声明,它是完全合法的 C.

于 2013-07-22T11:52:27.727 回答