This question already has answers here:
C: pointer to struct in the struct definition
(6 answers)
Closed 8 years ago.
I want to declare a self-referential structure as below
typedef struct
{
entry_t *entry;
node_t *next;
}node_t;
instead of as below for a linked-list
struct node
{
entry_t *entry;
struct node *next;
}*head;
does this work in C? If no why not?
It won't work, since the symbol/name node_t is unknown at the declaration of next:
typedef struct
{
entry_t *entry;
node_t *next; /* <-- Error: unknown type */
} node_t;
Your struct needs a name in its declaration in order to be "self referential". However, you can keep the typedef:
typedef struct node
{
entry_t *entry;
struct node *next; /* <-- type struct node is known */
} node_t;
Now you can use either struct node or node_t to create a new node.
The version you mentioned for the 2nd time is widely used, you can make a typedef like
typedef struct some_struct_name {
/* other fields, etc... */
struct some_struct_name *next;
} *some_type;
If you really, badly want that typedeffed name as early as declaring the structure itself, you can use a forward declaration using an incomplete type:
typedef struct node *node_t;
struct node {
int data;
node_t next;
};
This is also accepted by C89:
Related
I am a bit comfused about the row of these declarations. I want to make a linked-list for my program but for some reason it keeps putting error when I try to compile it. Basically this will be my main node model and I also have another struct after it (WITHOUT TYPEDEF) for my list. Which compiles just fine. I don't know what's wrong.
I have already tried to put the typedef over the struct student.
typedef struct
{
char name[50];
int id;
node next;
}student;
typedef student* node;
typedef struct listR* list;
struct listR
{
node head,tail;
int size;
};
error:
unknown type name 'node'
warning:
initialization make pointer from integer without a cast
The compiler doesn't know what a node is, because you create the node type after creating the structure.
You can do either :
typedef struct node node;
struct node
{
char name[50];
int id;
node* next;
};
To tell the compiler what a node is,
Or
typedef struct node {
char name[50];
int id;
struct node* next;
} node;
I would use this:
struct Node
{
struct Node *next;
struct Node *prev;
};
But then I happen to be one of those who does not like typedeffing structs without a good reason. Read more here: https://stackoverflow.com/a/54752982/6699433
Try to build a tree in C. The children has to be contained in a linkedlist. But when I use "struct listNode*" in the definition of struct treeNode, listNode is not declared yet. So is there any way to declare this first? Or anyway to get around this? Thanks!
/*** Build a tree ***/
typedef struct treeNode {
char* target;
char* commands;
struct listNode* children;
} tNode;
/*** Build a linkedlist ***/
typedef struct listNode {
struct treeNode dependency;
struct listNode* next;
} lNode;
Use what's called forward declaration. So your code should look like this
/*** Build a tree ***/
struct listNode;
typedef struct treeNode {
char* target;
char* commands;
struct listNode* children;
} tNode;
/*** Build a linkedlist ***/
typedef struct listNode {
struct treeNode dependency;
struct listNode* next;
} lNode;
Prepend the following to your snippet
struct listNode;
This is called a forward declaration.
The struct is not defined at this point, but the name is known, which is sufficient as you only want to reference it (with a pointer), not include it (which would require knowledge of its size).
Note that the restriction that you can only use pointers on yet-to-be-defined types actually makes sense: It makes
struct A {
struct B b;
int a;
};
struct B {
struct A a; /* Uh, what's that? struct B contains struct A
* which contains struct B... Now what's the size
* of either of these structs? */
};
an invalid construct (because it prevents circular dependencies).
I am studying code examples from my professor in order to become better acquainted with linked data structures.
In our linked-list.c example the professor defines a type Node as follows:
typedef struct node {
int data;
struct node *next;
} Node;
What's the point of the lower case node? I was under the impression that you could just write, for example:
typedef struct {
int data;
struct node *next;
} Node;
and then use Node as its own type. Does it have something to do with the fact that if you don't include a lower case node then when the compiler is evaluating the code it will not be able to understand what is meant by "struct node *next"?
Take a look at this declaration:
struct node {
int data;
struct node *next;
};
typedef struct node Node;
This can be combined into a single statement (simplifying a declaration):
typedef struct node {
int data;
struct node *next;
} Node;
Does it have something to do with the fact that if you don't include a lower case node then when the compiler is evaluating the code it will not be able to understand what is meant by "struct node *next"?
Yes.
The node in struct node is the tag of the struct type. If you give the struct a tag, you can refer to that type from the moment on the tag is complete, so in
typedef struct node {
int data;
struct node *next;
} Node;
the struct node *next; declares a member next that is a pointer to the struct type being defined. The typedef name Node is not available before the ; ending the definition is reached.
If you omit the tag, you cannot refer to the type being defined in any way before the typedef is complete, so in
typedef struct {
int data;
struct node *next;
} Node;
the line struct node *next; declares a new, unrelated, incomplete struct type with the tag node that next points to.
That's valid, but nothing about struct node is known (unless it is defined somewhere else), so you can't use the next pointer without casting it to a pointer to a complete type everywhere (not quite everywhere, Node foo; foo.next = malloc(12); etc. would still work).
He is defining a temporary name for the node because he is using a well know technique to avoid writing struct node on the declaration of each struct object.
If he would just do:
struct node {
int data;
struct node *next;
};
you would have had to use:
struct node* node;
to declare a new node. And to avoid that you would have to define later:
typedef struct node Node;
in order to be able to declare objects like the following:
Node* node;
In the end:
typedef struct node {
int data;
struct node *next;
} Node;
Is just a shortcut for struct node { ... }; in addition to typedef struct node Node;.
Here struct node is a type like int
and Hence
struct node {
int data;
struct node *next;
}NodeVar;
means you are declaring a single variable Node of struct node.
like int intVar;
typedef is to make your code understandable.
so that when you use
typedef struct node Node;
you can use the same declaration as
Node NodeVar;
Consider this code:
#include <stdio.h>
typedef struct {
int data;
struct node *next;
} Node;
int main()
{
Node a, b = {10, NULL};
a.next = &b;
printf("%d\n", a.next->data);
}
This won't compile. The compiler has no idea what a struct node is, other than it exists. So you might change the definition in the struct to Node *next;. The typedef isn't in scope before it's declared, so it still won't compile. The simple answer is to do as he said, use the node tag after struct, and it works fine.
The lower case 'node' is a structure type... i.e. a struct node { stuff } is a node structure containing stuff.
On the other hand, the upper case "Node" is a completely new data type which refers to a 'struct node'
Generally (though in C++ I think you can), you cannot pass around a "node" in a C program... for example as an argument to a function. Rather, you would have to pass a 'struct node' as your argument...
// this will throw a syntax error because "node" is not a data type,
// it's a structure type.
void myFunc( node* arg );
// while this will not because we're telling the compiler we're
// passing a struct of node
void myFunc( struct node* arg );
// On the other hand, you *can* use the typedef shorthand to declare
// passing a pointer to a custom data type that has been defined
// as 'struct node'
void myFunc( Node* arg );
I'm implementing a linked list in C. Here's a struct that I made, which represents the linked list:
typedef struct llist {
struct lnode* head; /* Head pointer either points to a node with data or NULL */
struct lnode* tail; /* Tail pointer either points to a node with data or NULL */
unsigned int size; /* Size of the linked list */
} list;
Isn't the "llist" basically useless. When a client uses this library and makes a new linked list, he would have the following declaration:
list myList;
So typing llist just before the opening brace is practically useless, right? The following code basically does the same job:
typedef struct {
struct lnode* head; /* Head pointer either points to a node with data or NULL */
struct lnode* tail; /* Tail pointer either points to a node with data or NULL */
unsigned int size; /* Size of the linked list */
} list;
You need to give a struct a name if you will reference it in its declaration.
typedef struct snode {
struct snode* next;
struct snode* prev;
int id;
} node;
But if you won't reference the struct inside it you dont need to give it a name.
EDIT
Notice that typedef is and struct are two different statements in C.
struct is for creating complex types:
struct snode {
struct snode* next;
struct snode* prev;
int id;
};
Which reads like make a structure called snode that stores two references to itself (next and prev) and an int (id).
And typedef is for making type aliases:
typedef struct snode node;
Which reads like make a type alias for struct snode called node.
Yes, you are correct. It is only a matter of habit or convention to explicitly name the struct in addition to the typedef.
Note that there is little to no cost either way, since llist is not a variable and does not take up memory. It is like the difference between naming a variable i or index - the compiled form is the same, but one may be more readable than the other.
It's useless in that particular case but, if you wanted a pointer to that struct within the struct itself, it would be needed.
That's because the struct is known at the opening brace while the typedef isn't known until the final semicolon (simplistic, but good enough here).
So you would need it for something like:
typedef struct sNode { // structure can be used now
int payload;
struct sNode *next; // cannot use typedef yet
} tNode; // typedef can be used now
You could turn this around: not the structure tag, but the whole typedef is superfluous.
struct snode {
struct snode* next;
struct snode* prev;
int id;
};
Now you can declare a pointer with:
struct snode *ptr;
You can even declare an array of them:
struct snode mynodes[10];
You'll have to type the struct keyword, but that won't hurt the compiler or the human reader (look at that syntax highlighting!).
You could even declare a pointer to an unknown type (at this moment of compilation) using an incomplete type:
struct xnode *xptr=NULL;
That will come in handy when you want to create an API to some library, where the actually implementtation of the library is not known to the caller:
struct gizmo *open_gizmo(char *path, int flags);
int fiddle_with_gizmo(struct gizmo *ptr, int opcode, ...);
Et cetera. A typedef would force the header file to "broadcast" all its internals to the caller, even if that is not needed.
Have a question about typedef in C.
I have defined struct:
typedef struct Node {
int data;
struct Node *nextptr;
} nodes;
How would I create typedef pointers to struct Node ??
Thanks !
You can typedef them at the same time:
typedef struct Node {
int data;
struct Node *nextptr;
} node, *node_ptr;
This is arguably hard to understand, but it has a lot to do with why C's declaration syntax works the way it does (i.e. why int* foo, bar; declares bar to be an int rather than an int*
Or you can build on your existing typedef:
typedef struct Node {
int data;
struct Node *nextptr;
} node;
typedef node* node_ptr;
Or you can do it from scratch, the same way that you'd typedef anything else:
typedef struct Node* node_ptr;
To my taste, the easiest and clearest way is to do forward declarations of the struct and typedef to the struct and the pointer:
typedef struct node node;
typedef node * node_ptr;
struct node {
int data;
node_ptr nextptr;
};
Though I'd say that I don't like pointer typedef too much.
Using the same name as typedef and struct tag in the forward declaration make things clearer and eases the API compability with C++.
Also you should be clearer with the names of your types, of whether or not they represent one node or a set of nodes.
Like so:
typedef nodes * your_type;
Or:
typedef struct Node * your_type;
But I would prefer the first since you already defined a type for struct Node.