Does multiple struct (Nested structure) exist in C? - arrays

I wonder if there is a way to declare multiple structs in C.
For example, I made this:
struct Team{
char TeamName[20];
int Point;
int Goals;
};
typedef struct TeamCup {
char GroupID;
struct Team team;
}Group;
Group g1, g2;
I want each TeamCup to have 4 teams. But when it comes to input process, in my loop, the variable here is undefined:
g1.Team[i].Point;

I want each TeamCup to have 4 teams
In this case you need to write
typedef struct TeamCup {
char GroupID;
struct Team team[4];
}Group;
and
g1.team[i].Point;
Thar is you need to declare an array of objects of the type struct Team within the structure struct TeamCup.

Related

Is it enough if i use one typedef struct?

I am writing a C program where I will use stack. My program will read the data from a file. I am asked to use typedef struct. Can I use typedef once just for stack? or should I hafve different 2 typedef struct like this:
typedef struct
{
int age;
float money;
char id[15];
} data;
typedef struct
{
float* data;
int member ;
}stack;
For starters this declaration
typedef struct
{
float* data;
int ;
}stack;
is invalid and in any case does not make a sense.
What you need is to define two structures. The first structure will declare a node of the stack as for example
typedef struct Node
{
int age;
float money;
char id[15];
struct Node *next;
} Node;
The second structure will declare the stack itself as for example
typedef struct Stack
{
struct Node *top;
} Stack;
And in main you can declare an object of the type Stack like
Stack stack = { NULL };
The function that pushes new elements on the stack can be declared like
int push( Stack *stack, int age, float money, const char *id );
typedef keyword is used to assign a new name to a type.
We can also use typedef to assign a new name to structure which is a user-defined datatype as follows:
typedef struct structure_name
{
data-type member-1;
data-type member-2;
data-type member-3;
data-type member-4;
}type_name;
Now, while declaring variables of this structure type, we can write type_name in place of struct structure_name in the whole program.
Can I use typedef once just for stack? or should I hafve different 2
typedef struct like this:
You have to use typedef for every structure, if you want to use just the type_name instead of struct structure_name.
Example : If we donot use typedef , then to create an instance of the structure , we need to use struct structure_name struct_instance.
If we use typedef , then we can just use typename struct_instance.

Function to compare structures in order to sort a list

I have an abstract data type in C, LIST OF THINGS, ist node has a void* pointer, what i'm trying to do is create a function to compare an specific field of different structures in order to sort my list of things.
typedef struct node{
char *name;
void *thing;
struct node *next;
}Node;
This is the node i'm working with, i've already created a list of integers, list of structures and te compare function for both, but i can't figure out how to do a compare function to diferent structures. for example:
given these types:
typedef struct main{
float weight;
char*model;
float maxspeed;
}Main;
typedef struct airplane{
float weight;
float maxspeed;
}Airplane;
typedef struct car{
char*model;
float maxspeed;
}Car;
And this is the function, so you have an idea of what i'm trying to do, it doesn't work, Main has fields that doesn't exist in either one or the other structure.
int comparefunction(void*a,void*b){
Main a1, a2;
a1=*(Main*)a;
a2=*(Main*)b;
return a1.weight-a2.weight;
}
This function(doesn't work) is passed as a paremeter to the function that links the nodes in order to use the comparefunction.
//insert prototype:
//insert(Node*listp,Node*newp,int(*func_comp)(void*,void*));
list=insert(list,newItem(&car1),comparefunction);
list=insert(list,newItem(&airplane1),comparefunction);
list=insert(list,newItem(&airplane2),comparefunction);
How can i do to compare a single field of two or more different structures? assuming that i know what each structure contains
If you're trying to compare somewhat similar things, you can look into unions.
struct attributes{
float weight;
// other common things?
};
struct thing {
enum { Car, Main, Airplane } type;
struct attributes attrs;
union {
struct Car car;
struct Main main;
struct Airplane airplane;
} other_thing;
};
You'd change your list to store the thing struct, which is a structure that encapsulates all your possible types. The common elements of each type are extracted to the attributes struct. Your compare function would then operate on the attributes struct of the thing struct. The union is used here to only create enough space within struct thing for the largest of the union elements, so that you're not wasting space storing all three structs and only using one.
Well, your car struct doesn't have a weight field, so I'm not exactly sure what you're trying to accomplish here. If you had your car struct look like
typedef struct car {
float weight;
char* model;
float maxspeed;
} Car;
I think your function would work. Note, it is important that the member that you want to compare is at the same offset into each struct including the Main struct.
EDIT
This does work.
Another edit based on comments
You can't compare two completely unrelated things. This is not a technical deficiency with C. It just does not logically make any sense to compare for example an Airplane and an int.

Create a list of structs the same size as another list in C

I have a list of students ordered for their registration number in C:
typedef struct student_type_ {
int number;
char name[20];
char surname[20];
struct stud_type_ *next_student;
} student_type;
and now I want to make another list of pointers to the elements of that list, that orders it alphabetically considering the name.
typedef struct sort_list_ {
struct student_type_ *student_data;
struct sort_list *next_student;
} sort_list;
Therefore I thought on first creating a new sort_list with the same size as student_type, but I don't get it right... is there a simple and elegant way to do this in C?
As a general design rule, always separate the data from the presentation of data. This will make your code cleaner and more elegant.
My suggestion: Make a student_data structure which contains only the student information. Then create two separate lists - one to sort by registration number and another to sort by name.
typedef struct student_data_ {
int number;
char name[20];
char surname[20];
} student_data;
typedef struct sorted_student_list_ {
struct student_data_ *student;
struct sorted_student_list_ *next;
} sorted_student_list;
...
sorted_student_list sorted_students_by_reg_number;
sorted_student_list sorted_students_by_name;

Use struct inside a struct in any order (C)

I have a very simple question: I want to use structs inside another structs but I want to be able to define them in any order I want.
Something like this:
// User type definition
typedef struct type1{
int i;
type2 t;
};
// User type definition
typedef struct type2{
int i;
type3 t;
};
// User type definition
typedef struct type3{
int i;
};
How can I do this?
The only way this can be accomplished is by using pointers to the structs instead of static members:
typedef struct type1 {
int i;
struct type2 *t;
} type1;
// User type definition
typedef struct type2 {
int i;
struct type3 *t;
} type2;
// User type definition
typedef struct type3 {
int i;
} type3;
The reason for this is the compiler MUST know how large the struct is as it gets to it. If you use pointers, all the compiler needs to know is that that struct type simply exists, since pointer types on a given architecture are a known size at compile time

Difference in declaring structures

I was using my structure like this. I don't like to typedef as I have told it can hide errors.
However, I was looking at some sample code and I have seen structures declared like this. And this is the normal way I declare them.
struct person
{
int age;
char name[32];
};
using like this:
struct person person_a;
person_a.age = 20;
etc.
However, I have seen structures declared like this:
struct
{
int age;
char name[32];
}person;
and
struct _person
{
int age;
char name[32];
}person;
What is the difference between all these different techniques, and how would you decide when it is the best to use each particular one.
Many thanks for any suggestions,
This:
struct
{
int age;
char name[32];
}person;
declares a variable person that is a struct. The struct has no name, but you still use the person variable the same way.
struct _person
{
int age;
char name[32];
}person;
This is identical to:
struct _person
{
int age;
char name[32];
};
struct _person person;
It declares a struct named _person and then makes a variable called person of type struct _person.
In both the last two structures, you've allocated storage for the structure called person.
What I mean is if you have something like this:
struct person
{
int age;
char name[32];
};
It's just a declaration; no variable allocation is there and hence cannot be used inside the code. You can start using this structure after you have declared as follows:
struct person p1;
Then, p1 can be utilized p1.age or p1.name, etc.
In terms of real-world code, instead of
struct _person
{
int age;
char name[32];
}person;
we usually see
typedef struct _person
{
int age;
char name[32];
} person_t;
In this case, we can save typing and more importantly the structure behaves like any other built-in type such as int, etc.
For example,
person_t p1;
person_t *p1;
And, so on.
For the first case (of second example), you will use this approach if you are sure that you do need another object for this structure. This looks clean and is fast to code :)
For the second case you have a ready object named 'person' and an option to create an object later on as
struct _person object2
This is to explain why you will use either approach. The difference between them has been explained well above.
There's no magic there really,
struct person
{
int age;
char name[32];
} var;
is the same thing as:
struct person
{
int age;
char name[32];
};
struct person var;
The 1st code just creates the person struct.
The 2nd code creates an instance of the person struct called person_a.
The 3rd code creates an unnamed struct and an instance of it called person.
The 4th code creates a struct named _person and an instance of it named person.
If the purpose of the structure definition is to declare a logical data structure that has meaning outside of the local module, then you would expect to see the first construct in a header file.
In real world C code, structures are often declared for purley pragmatic reasons that are local in scope. For example, formatting data that will be copied into a buffer. In these cases it's very handy and logical to merge the structure type and variable declaration. Also, using an anoymous declaration for the structure type avoids cluttering the namespace with a name that is not needed.
I do have one critisim of these examples: in C, names with a leading underscore are considered to be reserved for compiler vendors by convention. So I don't think the last example is consistent with best practice.

Resources