Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a structure: typedef struct name name;

Tags:

c

struct

typedef

We all know how to declare a structure in C:

struct Label1{ /* variables */ } Label2; // As I learned

But I want to know why this code works without declaring 'struct name':

typedef struct name s_name;

Or is in fact, does typing the code

struct name;

mean that I declared 'struct name' as a void structure or something like this?

Example of code:

typedef struct Data Data;
struct Data{ /*variables*/ };

If in the first line struct Data is declared as a void one, then in the second it's like I'm redeclaring it with members.

What is the explanation for this point?

like image 467
Michael Heidelberg Avatar asked May 23 '15 21:05

Michael Heidelberg


2 Answers

Something like:

struct MyStruct;

Is called a forward reference. It creates an incomplete type and tells the compiler there will be a type of that name (and it's a struct - it works likewise for unions), and the details "follow later". Of such a type you cannot define variables, until you complete the type.

typedef struct MyStruct MyType;

Will just define the type name to be that struct. This is still an incomplete type.

However, you can take a pointer to an incomplete type:

MyType *my_t_pointer;
struct MyStruct *my_s_pointer;

This is useful for a struct to have pointers to objects of the same type when you provide the full declaration, "completing" the type:

struct MyStruct {
    struct MyStruct *next;
};

Actually this is the only way to create nodes for lists, trees, and all other recursive data-structures. This is a major part of C programs (sometimes hidden).

Also, this mechanism is used to hide implementation details. Functions in the header need only know the struct exists to take/pass pointers to it. The use of these functions need not to know the details of the struct (but this way it cannot allocate it, so the module has to cover all aspects which need to know details on the struct). The full declaration is only inside the implementation file of the module. These pointers are called "opaque" as one cannot "look through", i.e. access the fields of the struct as they are simply not known to it.

my_module.h:

struct MyStruct;

extern void my_init(struct MyStruct *obj);

my_module.c:

struct MyStruct {
    int f1;
    ...
};

my_init(struct MyStruct *obj)
{
    ...
}
like image 145
too honest for this site Avatar answered Oct 15 '22 03:10

too honest for this site


The typedef declares s_name as an alias for struct name so that you can declare variables, e.g.:

s_name *sptr;

The line

struct name;

declares that there is a struct type called name without defining its content. This is usually done in order to be able to declare variables as pointers to the struct type. You cannot declare variables of the actual struct type until it has been defined.

like image 20
Bjorn Munch Avatar answered Oct 15 '22 04:10

Bjorn Munch