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
Related
I have two seperat files
in main function file 1:
struct entity{
unsigned int pos_x;
unsigned int pos_y;
unsigned int health;
} player;
struct inventory{
char helmet[length_item_names];
char body[length_item_names];
char legs[length_item_names];
char items[inventory_space];
} items;
render_screen(rendered_size_x, inventory_size_x, screen_size_y, map, player, items);
file 2:
void render_screen(int render_size_x, int inventory_size_x, int screen_size_y, char **field, struct entity player, struct inventory items){
...
}
when declaring render_screen i either get a error telling me that the struct entity and struct inventory can't be declared as incomplete data types or when i do
extern struct Player{
...
}
it tells me that the data type i give as a parameter is wrong
how can i give the struct as a prameter
The comment of Barmar workerd.
I had to put the declaration of the struct into a header file that i had to include in both c files.
Thanks for the quick help
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)
I read a tutorial in which there is this struct:
struct
{
char Name[25];
int Age;
float SkillRating;
} Employee;
defines a new aggregate, called Employee, containing fields called Name (of type character), Age (of type integer), and SkillRating (of type float).
In contrast, the C statement:
struct EmployeeType
{
char Name[25];
int Age;
float SkillRating;
};
does not define a new aggregate variable, but defines a new aggregate type,
EmployeeType.
This new data type could then be used to declare variables in the
same way as a primitive data type. That is, in the same way that C allows the
variable x to be declared as an integer using the statement
I am confused here. Does the distinction exist if place 'Emplyee` on different position?
I guess they are identical.
In the first case, the struct is unnamed and Employee is a variable of that unnamed struct. You can directly modify it like this:
int main()
{
Employee.Age = 100;
return 0;
}
In the second case, EmployeeType is just a type, but you didn't make any instance of it yet. You can make any amount of instances:
int main()
{
struct EmployeeType a; // employee on the stack
a.Age = 100;
struct EmployeeType *b = malloc(sizeof(struct EmployeeType)); // employee on the heap
if (b) { // set the age of b if the allocation succeeded
b->Age = 100;
}
free(b); // malloc-allocated memory must be freed afterwards
return 0;
}
You can even do both at once:
struct EmployeeType
{
char Name[25];
int
Age;
float SkillRating;
} Employee;
Here, Employee is one instance of it, but you can make additional instances:
int main()
{
Employee.Age = 100;
struct EmployeeType a; // employee on the stack
a.Age = 100;
return 0;
}
struct A_s { int memb; }; // declares a struct A_s
struct A_s a; // declares a variable a of type struct A_s
now you can combine struct declaration with variable declaration:
// declares struct A_s and in the same line declares variable a
struct A_s { int memb; } a;
Now you can create an anonymous struct by omitting the structure tag name:
// declares anonymous struct and in the same line declares variable a
struct { int memb; } a;
Now structure declaration can be really put anywhere:
int func(int a, struct A_s { int memb; } b);
struct A_s { int memb; } func(int a);
struct B_s {
int memb1;
struct A_s {
int memb2;
} a;
};
I think the description of the C code of the field "name" in your post is invalid. The field name inside "aggregate" (read as: structure) has the type "array of 25 characters", not character type.
struct keyword in C can be used to declare aggregate datatypes, aggregate data objects or both.
In your first declaration (and definition), a tag name is missing, so an anonymous object 'Employee' is created. 'Employee' remains the only object of this struct. You cannot create more objects from this struct anywhere else in the code.
In your second declaration, you have created a struct type which can be instantiated many times (i.e., multiple instances of this struct can exist) as shown below -
struct EmployeeType employee_1;
struct EmployeeType employee_2;
Both these syntax are useful depending on the use case.
Hi I am trying to create a structure inside a structure, the first structure will contain details for a number of students, the second structure will contain details of a number of modules from each student. Each time asking the user to input details for each student. I was able to create an instance of student structure called data and each time loop through each instance of data and scan in information for each student once only that worked for me. I am now trying to create another inside loop that will scan in a number of different modules that will assign these to each student. I am having problems with the second loop and would appreciate some guidance.
struct module{
char moduleid[5];
int credit;
float grade;
};
//create structure student at includes a datatype
//module
struct student{
char id[10];
char fname[15];
char lname[15];
struct module results;
};
int main()
{
int i;
int j;
int numStuds;
int numMods = 10;
printf("Enter number of students\n");
scanf("%d",&numStuds);
struct student data[numStuds]; //create an instance of structure student called data
struct data.results mods[numMods];
//input data
for (i=0; i<numStuds; i++){
printf("PLEASE ENTER STUDENT DATA :\n\nID, FNAME AND LNAME\n");
scanf("%s%s%s",data[i].id,data[i].fname,data[i].lname);
for (j=0; j<numMods; j++){
printf("\nENTER MODULE DATA: \nMODULEID, CREDIT, GRADE :\n");
scanf("%s%d%f",data[i].results.mods[j].moduleid,&data[i].result.smods[j].credit,&data[i].results.mods[j].grade);
}
}
I think that your main problem is that in the way you have declared "results" inside your student struct you can only have one result for each student. Try to use an static array (if you know how many results you have) or a dynamic one if you want to declare its size during runtime.
e.g
struct student{
char id[10];
char fname[15];
char lname[15];
struct module *results; //Pointer to declare a dynamic array
};
For static declaration
struct student{
char id[10];
char fname[15];
char lname[15];
struct module results[size]; //Pointer to declare a dynamic array
};
If you use the pointer solution remember to access that attribute using "->" operator
Like acostela said, you might want a pointer to a dynamic array. However, I do not see why each student needs multiple results. So when you have the number of students entered, you would just know that each student had one module, referred to as data.result or data->result if you used malloc. If you really need dynamic arrays, I can show you some macros to do this easily.
Okay. I will just show you creation and deletion macros, you can do expansion, inserting, and deleting with realloc and memmove; it would take me too long to write them all out.
#include <stdint.h>
#define ARRAY_DECL(name, type) struct { uint32_t count; type *array } name;
#define ARRAY_DESTROY(name) free(name.array);
These macros create what is called an anonymous structure; this means that it is a one-of-a-kind struct, in this case called name. ARRAY_DESTROY then frees the array inside the struct.
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++