Hello,

this type of structures are confusing. I hav been working with C for a while but this subtlety is yet unknown.

I have come accross such a structure in a program where suposedly it will be used for FIFO management:

typedef struct FIFO_TAG{
    struct FIFO_TAG  *NextElement;
}FIFO;

And then FIFO would be used.

The thing is that I am trying to make equivalents of structures into Python objects and recover the structure parameters into object attributes, but I need to understand this structure's functionalities in order to do that at best, and I can't so far....not well enough anyway..

Could anyone please explain to me how the inside parameter gets its value and how this structure could be used?It is used in some functions with pointers for FIFO behavior, but if I can intuitively undertand those, I can't see how C interprets such an architecture...

thanks in advance,

T

Hello,

typedef struct FIFO_TAG{
    struct FIFO_TAG  *NextElement;
}FIFO;

And then FIFO would be used.

That example is rather useless without any data fields in FIFO_TAG, but may be you've dropped ones for clarity.

First of all, FIFO is simply an alias, so the declaration may be rewritten as

struct FIFO_TAG{
    struct FIFO_TAG  *NextElement;
};

typedef struct FIFO_TAG FIFO;

Struct FIFO_TAG contains pointer to struct FIFO_TAG, that generally used for dynamic memory allocation for next element, that contains pointer to next element and so forth... For example:

struct FIFO_TAG* top;
top = malloc(sizeof(*top));
top->NextElement = malloc(sizeof(*top));
top->NextElement->NextElement = NULL;

What happens here? We now have somewhere in memory two dynamically linked structs -- first is referenced as top. Really it is a NULL-terminated linked list with two elements.

In that way any of the fundamental data structures -- such as linked lists, queues, trees, etc may be expressed in C. I may recommend you a book "Mastering Algorithms with C" by Kyle Loudon, where you may read about that in more detail.

commented: Very helpul, thanks a lot, others will learn more about such situations too, I'm sure. +1

That pointer is used to create a linked list of structures -- each pointer points to another structure in the list. Study linked lists and you will find out how all that works. There are many tutorials available, such as this one.

commented: Great tutorial...Thanks +1

Thanks very much to both of you, it's much clearer when it's exposed with calm that just my intuition...I haven'tworked with queues or other such linked structures in C so far...so I didn't now how they were put in place...

Thanks again,

T

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.