Initializing pointers of structs through malloc - c

This is my following structs:
struct team {
char* group_name;
Driver driver1;
Driver driver2;
} ;
struct season {
int year;
int groupNums;
Team *teamArray;
int numOfDrivers;
Driver *driverArray;
};
this is how I initialized them each suitable header
typedef struct season* Season;
typedef struct team* Team;
I initialized the teamArray like this
season1->teamArray = (Team *)malloc(sizeof(Team)*teamNums);
ps: season1 is an input of type Season
Is this the right way to do it?

Related

Does multiple struct (Nested structure) exist in C?

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.

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.

Multiple structure pointers inside another structure

I am writing code to create a team of players
struct player {
char name[32];
double avg;
};
struct team {
char teamname[32];
player *players[11];
};
I get the error:
A4.c:31:3: error: unknown type name ‘player’
player *players[11];
I've looked elsewhere on the internet and I can't seem to find out how I would store multiple player pointers inside the team structure?
struct player {
char name[32];
double avg;
};
In the above code, player is a struct name, not a type. So the correct definition of team is:
struct team {
char teamname[32];
struct player *players[11];
};
Alternatively, you can define a new player type:
typedef struct player {
char name[32];
double avg;
} player;
struct team {
char teamname[32];
player *players[11];
};
Note: In C++, the struct keyword is optional before in declaration of a variable. In C, it is mandatory. (https://www.geeksforgeeks.org/structures-in-cpp/)

struct array inside another struct

I'm trying to create an structure with other structures inside.
struct bullet{
char bullet_sprite[100];
int pos_x;
int pos_y;
int ace_x;
int tag;
};
struct bullets_onscreen{
struct bullet v[2];
struct bullet a[2];
};
I get this error:
error: array type has incomplete element type
Is this posible to do?
Example code:
//Calling functions
struct bullets_onscreen[2] //public
struct bullet bala[1];
init_bullet(&bala,_player);
set_bullet_on_screen(&bala);
void set_bullet_on_screen(struct bullet *_bullet){
array_bullet[1] = _bullet;
}
void init_bullet(struct bullet *_bullet, struct player *_player){
//inits all bullet components
}
As written your code is fine. Presumably in the actual code you have reversed the order of the two struct definitions. This code produces the error you report:
struct bullets_onscreen{
struct bullet v[2];
struct bullet a[2];
};
struct bullet{
char bullet_sprite[100];
int pos_x;
int pos_y;
int ace_x;
int tag;
};
Define the structs in the order that you did in the question and your code will compile.

Expected specifier-qualifier-list before 'rooms'

I'm working on a DnD-Text-Based-Style C game, and I'm having a problem compiling my structure.
Here is what I have for a structure so far:
typedef struct stats { //
int strength; //
int wisdom; //
int agility; //
} stats;
typedef struct rooms {
int n_monsters;
int visited;
rooms nentry;
rooms sentry;
rooms wentry;
rooms eentry;
monster *monsters;
} rooms;
typedef struct monster {
int difficulty;
char *name;
char *type;
int hp;
} monster;
typedef struct dungeon {
char *name;
int n_rooms;
rooms *rooms;
} dungeon;
typedef struct player {
int maxhealth;
int curhealth;
int mana;
char *class;
char *condition;
stats stats;
rooms c_room;
} player;
typedef struct game_structure {
player p1;
dungeon d;
} game_structure;
When I compile it, I get the error:
structure.h:21: error: specifier-qualifier-list before 'rooms'
Can you help me figure out why this is? Is it because I'm calling rooms from the structure that contains rooms? Please help.
There are many many problems with this piece of code. The first is that a struct definition must know how much memory to allocate, and it cannot do this when it contains a type that has not been fully defined (this is why people recommend that you use a pointer, since the size of a pointer is known at compile time).
However, and this is important, simply changing to pointers will not solve the problem, since the first room* is encountered before the appropriate typedef completes. You would need to write something like:
struct rooms {
int n_monsters;
int visited;
struct rooms *nentry;
struct rooms *sentry;
struct rooms *wentry;
struct rooms *eentry;
monster *monsters;
} rooms;
or perform a forward declaration (typedef struct rooms rooms;). You also need to make sure the monster type is defined or at least has a forward declaration.
You can't have a variable with the same name as a type. Your dungeon structure has this field entry:
rooms *rooms;
Change that variable name (or, alternatively, rename the type) to something else. What you have now is analogous to:
int int;
Which is clearly not going to work!
typedef struct rooms {
int n_monsters;
int visited;
rooms nentry;
rooms sentry;
rooms wentry;
rooms eentry;
monster *monsters;
} rooms;
Looks like rooms is a recursive type, you certainly wanted to use pointers instead:
typedef struct rooms rooms;
struct rooms {
int n_monsters;
int visited;
rooms *nentry;
rooms *sentry;
rooms *wentry;
rooms *eentry;
monster *monsters;
} rooms;

Resources