Problems with typedef struct name [duplicate] - c

This question already has answers here:
typedef struct vs struct definitions [duplicate]
(12 answers)
Why should we typedef a struct so often in C?
(15 answers)
Closed 17 days ago.
Snippet 1:
typedef struct list_s {
int key;
struct list_s *next;
}list_t;
Snippet 2:
typedef struct list_s list_t;
struct list_s {
int key;
list_t *next;
};
I don't understand in snippet 1 or snippet 2 if the name of the struct is list_t or list_s. Usually I define a struct with name other_t as:
typedef struct{
int n;
}other_t;
Also in snippet 1 and snippet 2, what does the referencing to the struct with a pointer, which it is inside of meant to do? I mean :
struct list_s *next;
What is it's function?

This is how one defines a struct type:
struct Foo {
...
};
This can be used as follows:
struct Foo foo;
struct Foo *p;
Using typedef, we can create an equivalent and compatible type without having to use struct everywhere.
typedef struct {
...
} Foo;
This can be used as follows:
Foo foo;
Foo *p;
When creating a self-referencing type (as snippets 1 and 2 do), you have to (directly or indirectly) use a struct type and not a "generic" type.
struct Foo {
struct Foo *next;
};
But nothing stops from also creating a "generic" type.
typedef struct Foo Foo;
(They can even have the same name, as is the case in my example.)
Snippets 1 and 2 are equivalent, creating both a struct type (struct list_s) and an equivalent "generic" type (list_t).
Snippet 3 creates only a "generic" type (other_t).
The structure is probably used as the type for the nodes of a linked list. The pointer in one node would point to the next node in the list.
list_t *head list_t anon0 list_t anon1 list_t anon2
+------------+ +------------+ +------------+ +------------+
| ---------->| key: ... | +-->| key: ... | +-->| key: ... |
+------------+ +------------+ | +------------+ | +------------+
| next: -------+ | next: -------+ | next: NULL |
+------------+ +------------+ +------------+

Related

How to understand pointer 'next' in a linked list structure?

struct Node
{
int a;
struct Node *next;
};
How will next address point dynamically? I read malloc returns address value — is that right?
Please explain struct Node *next. Is this the default way of declaring a pointer in a struct?
If you have this declaration
struct Node
{
int a;
struct Node *next;
};
then you can define it like so:
struct Node node = {1, 0};
or
struct Node *node = (Node*) malloc(sizeof(struct Node));
When you want to attach a node to the next member then you can like so for example:
node.next = (Node*) malloc(sizeof(struct Node));
or
node->next = (Node*) malloc(sizeof(struct Node));
Example experiment:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
struct Node
{
int a;
struct Node *next;
};
struct Node node1;
struct Node *node2 = (Node*) malloc(sizeof(struct Node));
node1.a = 1;
node2->a = 2;
node1.next = node2;
node2->next = (Node*) malloc(sizeof(struct Node));
node2->next->a = 3;
printf("node1.a = %d, node1->next->a node2->a = %d, node2->next->a = %d\n", node1.a, node2->a, node2->next->a);
}
Yes your declaration is correct. To understand it, see it this way. When the compiler wants to know what kind of pointer it should compile the strcut's next field. The declaration type you gave is used. Since the compiler already parses the structure before coming to this line. It understands that the next pointer type is also of same structure type. I hope this helps in your understanding.
Start points to the top of the list and is available globally to your program. Whereas next just keeps track of the next item, and is available when referring to a specific 'node'. See this diagram it may help you understand with a visual!
link internally tracks the following item which keeps track of where the next component is as it is not necessarily contiguous the way arrays are.
+------+ +------+ +------+
| data | | data | | data |
+------+ +------+ +------+
| next |---->| next |---->| next |----> NULL
+------+ +------+ +------+
^
|
START (Keep track of the whole list.)
Hope that helps clarify.

data accessing isuue using pointer to the next node in linked list [duplicate]

This question already has answers here:
LinkedList Struct Typedef in C
(3 answers)
Closed 7 years ago.
//first_link_list.c This code Is not working
1 #include<stdio.h>
2 #include<stdlib.h>
3 typedef struct{
4 int data;
5 struct link *next;
6 }link;
7 int main(){
8 link *head = malloc(sizeof(link));
9 link *first = malloc(sizeof(link));
10 link *second = malloc(sizeof(link));
11 head->data = 35;
12 head->next = first;
13 first->data=45;
14 first->next=second;
15 second->data = 55;
16 second->next = NULL;
17 printf("VALUE of first element %d\n",*(first->next));
18 return 0;
19 }
When I run 1st code It Gives Error/Warning Like This
first_link_list.c: In function main
first_link_list.c:12: warning: assignment from incompatible pointer type
first_link_list.c:14: warning: assignment from incompatible pointer type
first_link_list.c:17: error: dereferencing pointer to incomplete type
What is went wrong,if i use "struct" instead of "typedef" the code works properly what is wrong in using typedef
If you want to point to a struct inside the same type of struct you can name it before its contents like:
typedef struct link
{
int data;
struct link *next;
}
link;
In C, structs have a separate namespace. when you changed to a typedef, the struct has a typedef name, but no struct name. You can add a struct name:
typedef struct link_struct {
int data;
struct link_struct *next;
} link;
Now you can refer to the struct type using either struct link_struct or the typedef name link.
Since structs have a separate namespace, you could use the name link for both the struct and the typedef. However, in C++ structs do not have their own namespace, so if you use the same name you cannot compile the code as C++.

Self Refrential Stacks

Suppose we have a stack as defined below:
typedef struct node
{
int data;
struct node *next;
} node;
I copied this definition from a book but I'm not exactly sure what the last 2 lines do. Does the node declaration at the end have anything to do with the structure declaration or is it just an object that we are creating?
The code is equivalent to:
struct node
{
int data;
struct node *next;
};
typedef struct node node;
By separating typedef with the struct node type, it's easier to see node in your code is an alias for struct node made by typedef.
considering last two line:
1:
struct node *next;
explains: you are creating a pointer variable (i.e, next) of type struct node and capable of holding address of the same type. usually the concept comes when you want implement the stack operation dynamically.
+--+----+ +--+----+ +--+----+ +--+----+
|10|2000| |20|3000| |30|4000| |40|NULL|
+--+----+ +--+----+ +--+----+ +--+----+
1000 2000 3000 4000
it can be seen that the next part is handling the address of the another structure and hence you need the pointer of type struct node.
2:
typedef struct {
type1 fieldname1;
type2 fieldname2;
....
typeN fieldnameN;
} NEW_TYPE;
NEW_TYPE variable_identifier;
Here the NEW_TYPE is the typedef name can be used like any type.
I have tried giving the generic syntax. hope you get it.

what is the purpose of adding "struct name *next" inside "struct name" in linked list. I mean i actually cant process what exactly happens

#include<stdio.h>
#include<stdlib.h>
typedef struct employ
{
int reg;
int sal;
char *name;
struct employ *next; // I want to know the purpose of this line
}EMP;
int main(void)
{
EMP *first,*emp1,*emp2,*emp3,*ans;
first = (EMP *)malloc(sizeof(EMP));
first->next = NULL;
/////first///////
emp1 = (EMP *)malloc(sizeof(EMP));
if(emp1 == NULL)
{
perror("malloc error");
exit(1);
}
emp1->reg = 100;
emp1->sal = 30000;
emp1->name = "james";
emp1->next = first;
first = emp1;
/* I havent completed the program bcoz its not necessary here */
In a linked list, each node contains a pointer to the next node.
struct employ *next;
is used to achieve that effect
+---------+ +---------+
| Node1 |------->| Node2 |----> ...
+---------+ +---------+
if you are in node1, next is a pointer to node2, that way you can accesss next element from the current element
A linked list is a data structure consisting of a group of nodes which together represent a sequence. Here the reg, sal and *name represent your data structure and *next will hold the link to next element in the list.
Consider the following data structure
struct sample {
int number;
struct sample *next;
};
The pictorial representation for this structure would be
The above example has 3 elements, every element is an individual structure variable. In the above example the variable 1 has members number and *next. Here, the number contains 12 and the *next contains the address of the structure variable holding the data 99. Since the type of the variable holding the data 99 is of type struct sample that is why we take *next type as struct sample type.

self referential structures in C [duplicate]

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:

Resources