Struct ⇒ Model Real-world entities!
#include <stdio.h>
#include <stdlib.h>
struct Student{
char name[50];
char major[50];
int age;
double gpa;
};
int main()
{
struct Student student1; // Creating a struct container named 'student1'//
student1.age = 22;
student1.gpa = 3.2;
strcpy( student1.name, "Jim");
strcpy( student1.major, "Business");
struct Student student2;
student2.age = 20;
student2.gpa = 2,5;
strcpy( student2.name, "Taylor");
strcpy( student2.major, "Art");
printf("%f", student1.gpa);
printf("%s", student2.name);
return 0;
}
In C, a string is just an array of characters, in which we cannot just assign a value to it (i.e. each element of arrays has a value, but the whole array cannot simply have just one value).
Hence,
student.name = "Bill";
cannot be done.
We must use strcpy() function instead.