iAmoric
iAmoric

Reputation: 1975

Need help to understand structs in C

I need some help to understand this struct:

typedef struct std_fifo{
    char* name;
    struct std_fifo* next;
}std_fifo, *fifo;

With typedef I know that I can use only std_fifo instead of writing struct std_fifo in my code. But what about the *fifo?

Upvotes: 2

Views: 123

Answers (2)

John Bode
John Bode

Reputation: 123598

The code

typedef struct std_fifo{
    char* name;
    struct std_fifo* next;
}std_fifo, *fifo;

creates two (very badly named) typedef names, std_fifo and fifo.

The typedef name std_fifo is equivalent to the type struct std_fifo, and can be used in place of struct std_fifo:

std_fifo fifo_instance;      // creates an instance of struct std_fifo
std_fifo get_fifo();         // declares get_fifo as a function returning an 
                             // instance of struct std_fifo
void read_fifo( std_fifo * );// declares a function taking parameter of type
                             // pointer to struct std_fifo

The typedef name fifo is equivalent to the type struct std_fifo *, and can be used in place of struct std_fifo *:

fifo fifo_ptr;               // creates a pointer to an instance of struct std_fifo
fifo get_fifoptr();          // declares get_fifoptr as a function returning a pointer
                             // to an instance of struct std_fifo
void read_fifo( fifo );      // declares a function taking a parameter of type
                             // struct std_fifo *

The reason code like

typdef struct std_fifo { ... } std_fifo;

works is because C has four different name spaces for identifiers: labels, tag names, struct and union member names, and everything else. The tag name std_fifo is always preceded by the struct keyword, which is how the compiler distinguishes it from the std_fifo typedef name.

Some advice on using typedefs:

While they can help your code scan better in some cases, using typedefs can actually obscure your intent and make types harder to use. If the user of the type has to be aware of its representation (such as to access a member of a struct, or dereference a pointer type, or to use the right conversion specifier in a printf or scanf call, or to call a function propertly, etc.), then you should not hide the representation behind a typedef.

If you decide you do want to hide a type's representation behind a typedef, then you should also provide a full API for any operations involving that type. C does this with the FILE type; instead of manipulating a FILE object directly, you pass a pointer to it to the various stdio routines. So, if you decide you want to hide struct std_fifo * behind the typedef name fifo, then you should also create an API like:

fifo new_fifo();                // create a new fifo queue
void destroy_fifo( fifo * );    // destroy an existing fifo queue
set_name( fifo, const char * ); // set the name of a fifo element 
char *get_name( fifo );         // retrieve the name of a fifo element
fifo push_fifo( fifo );         // add an element to the end of the queue
fifo pop_fifo( fifo );          // remove an element from the front of the queue

Abstraction can be a Good Thing, but "leaky" abstractions are worse than no abstraction at all.

Upvotes: 3

Prometheus
Prometheus

Reputation: 1532

It is a valid definition of struct to give either with a name and with a pointer.

typedef struct std_fifo{
    char* name;
    struct std_fifo* next;
}std_fifo, *fifo;

In this code, where std_fifo is a struct and *fifo is the pointer to this struct.

I would strongly suggest you to take a look at here : http://www.cplusplus.com/forum/windows/57382/

Upvotes: 1

Related Questions