Why does "struct T * next" compile when T is not an existing type?

I am using MinGW for Windows. I am creating a linked list and I am confused by this.

#include <stdio.h> #include <stdlib.h> typedef struct Data { int x; int y; struct BlaBla * next; /*compiles with no problem*/ }List; int main(void) { List item; List * head; head = NULL; return 0; } 

Now I cannot have a struct struct (object, instance of this struct), but it can have a pointer to this type of structure. I did not know that a pointer could be a pointer to an unused type. struct BlaBla * next ; (not for a linked list, it should be struct Data * next , but means a general conversation)

+4
source share
4 answers

Yes, you can, because then the compiler, faced with an unknown type name for the first time, assumes that there was a definition of the structure type with that name. Then it will be the forward-declare structure name for you, allowing you to use it as a pointer, but you cannot cast it and you cannot perform pointer arithmetic on it (since this is an incomplete type).

+6
source

The compiler will accept the code, for example your example:

 typedef struct Data { int x; int y; struct BlaBla * next; /*compiles with no problem*/ }List; 

This is normal because the size of the pointers is known to the compiler, and the compiler assumes that the structure will be defined before it is dereferenced.

Since the compiler acts in this way, this can be done:

 typedef struct Data { int x; int y; struct Data * next; /* points to itself */ } List; 

However, if you must enable struct inline, like this:

 typedef struct Data { int x; int y; struct BlaBla blaStruct; /* Not a pointer. Won't compile. */ }List; 

The compiler cannot decide how large the struct Data is because it does not know how large the struct BlaBla . To get this for compilation, you need to include the struct BlaBla .

Note that as soon as you need to access the members of the struct BlaBla , you will need to specify a header file that defines it.

+3
source

It depends on what you mean by "untrue." If you have not even declared BlaBla , you will receive an error message.

If you have stated this, but have not yet defined it, this will work fine. You are allowed to have pointers to incomplete types.

In fact, the usual way to make opaque pointers in C.

So you might think that this is unacceptable because there is no struct BlaBla in scope:

 typedef struct Data { struct BlaBla *next; // What the ?? } List; 

However, this is really normal, as it simultaneously declares a struct BlaBla and defines next .

Of course, since the definition implies a declaration, this is also good:

 struct BlaBla { int xyzzy; }; typedef struct Data { struct BlaBla *next; // What the ?? } List; 
0
source

To declare a variable or field of this type, pass one of them as a parameter or copy each other of the same type, the compiler must know how many bytes the variable or field occupies, what alignment requirements (if any) and other types of pointers, with which it is compatible with, but what all compilers should know about it. In all common C dialects, a pointer to any structure will always be the same size and requires the same alignment, regardless of the size of the structure that it points to or what this structure may contain, and pointers to any type of structure are compatible with other pointers with the same and the same type of structure.

Therefore, the code, which should not do anything with pointers to the structure, except for allocating space for storing the pointers themselves [unlike the structures in which they point], passes them as parameters or copies them to other pointers, you do not need to know anything about the type of structure to which they indicate, in addition to their unique name. Code that should allocate space for a structure (as opposed to a pointer to one) or access to any of its members should know more about its type, but code that does not perform these actions does not need such information.

0
source

All Articles