Multiple structure pointers inside another structure - c

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/)

Related

How to store data in struct member which is also a pointer?

This is the struct that I have to use
struct subject {
char subjectID[10];
float marks; };
struct student {
char name[20];
char ID[10];
struct subject *enrolled; };
My program has to let the students enroll in more than one subject, how do I store this???
Consider adding 2 elements to your structure student.
Add a dynamic array of subject and a enrolled_count and using that track enrolled subjects.
An example:
struct Student
{
char name[20];
char ID [10];
struct subject *enrolled;
int enrolled_count;
}
So when you access it somewhere you can use Student.enrolled[index] (the . can be -> depends if it's a pointer to a struct)

Initializing pointers of structs through malloc

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?

different formats to represent the structures

I have watched videos by different people on youtube and now i am little bit confused
first two[(1) (2)] are of same type without typedef and then two[(3) (4)] are of same type with typedef but the way of writing them is different
Please mention if any of the below represented structure is wrong and why with number. I am assigning number to all of the these formats
(1)
struct tag{
int number;
char *name;
float num;
};
main()
{
struct tag name;
name firstmember;
------code-------
return 0;
}
Next type i have seen is same as the above but :
(2)
struct tag{
int number;
char *name;
float num;
}name;
main()
{
name firstmember;
------code-------
return 0;
}
Next is using structure with typedef:
(3)
typedef struct tag{
int number;
char *name;
float num;
}name;
main()
{
name firstmember;
------code-------
return 0;
}
same version of typedef with other one:
(4)
struct tag{
int number;
char *name;
float num;
};
main()
{
typedef struct tag name;
name firstmember;
------code-------
return 0;
}
Please mention if any of the the above represented examples have any kind of error
Lastly this doubt is completely different from the above examples
if we don't put a structure tag just after the word struct and do like this
(5)
struct {
int number;
char *name;
float num;
}tag;
main(){}
does this word 'tag' remains structure tag or if its after '}' so its a structure name now ? What is it? If it becomes the name why do we use the structure tags anyways?
Please respond to this question after reading all my doubts carefully in well written format
Have a look at these close threads with popular answers:
Difference between 'struct' and 'typedef struct' in C++?
typedef struct vs struct definitions
Why should we typedef a struct so often in C?
struct and typedef in C versus C++

What this Declaration meant in C

I have a structure in C and at end have some declration which not able to decode
struct Student
{
int roll;
char name;
int age;
};
extern struct Student dev[];
what does last statement mean in C??
extern struct Student dev[];
Tells the compiler that dev is an array of the type struct Student and it is defined somewhere else(other translation unit).
It means that dev[] is not declared in this C/object file, but in another. You'll have to link that other object to your binary to be able to use that variable.
struct students
{
int num;
char name[100];
char dept[100];
} extern struct students student[];
student[] is the structure array.its used for the access the structure members
like num,name,dept.
int j=100;
#include<stdio.h>
main(){
for(i=0;i<j;i++)
{
scanf("%d",&student[i].num);
scanf("%s",student[i].name);
scanf("%s",student[i].dept);
}
for(i=0;i<j;i++)
{
printf("%d\n",student[i].num);
printf("%s\n",student[i].name);
printf("%s\n",student[i].dept);
}
}
It is used to the access the 100 records of members of the structure

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