C typedef for undeclared structure does not throw any compilation error

I'm just wondering how typedef doesn't throw any compilation error when using it with an undeclared structure. Below code is compiled without any warnings and errors. My doubt is why a typedef with an undeclared structure does not cause any error. The same on all platforms

 #include <stdio.h> typedef struct undeclared_struct_st UND_STRUCT_S; int main() { printf("\nhello world\n"); return 0; } 

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

+4
source share
2 answers
 typdef struct undeclared_struct_st UND_STRUCT_S; 

. It declares struct undeclared_struct_st as a non-return type, and then declares UND_STRUCT_S as a typedef for struct undeclared_struct_st . You cannot create objects of an incomplete type, but you can create pointers to objects of an incomplete type. struct undeclared_struct_st can be declared in another translation unit.

+8
source

This is called a forward declaration, and it is perfectly legal C.

+5
source

Source: https://habr.com/ru/post/1492691/


All Articles