I have this two different code writing in C. Both of them just try to assign value into structure.
And i'm using cs50 library to help me to get string in simple way.
For the first code, when i'm trying to execute it got an error.
First Code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#define MaxStudents 5
typedef struct
{
string name;
int age;
string gender;
}
student;
student students[MaxStudents];
int main(void)
{
students[0] = {"Kevin Mahendra", 22, "Male"};
students[1] = {"Wasis Sirutama", 22, "Male"};
students[2] = {"Alief Dean", 22, "Male"};
students[3] = {"Adwi Lanang", 21, "Male"};
students[4] = {"Dhimas Kuncahyo", 22, "Male"};
for (int i = 0; i < MaxStudents; i++)
{
printf("%s, %i %s", students[i].name, students[i].age, students[i].gender);
}
}
Problem (first code):
sorting.c:19:19: error: expected expression
students[0] = {"Kevin Mahendra", 22, "Male"};
^
sorting.c:20:19: error: expected expression
students[1] = {"Wasis Sirutama", 22, "Male"};
^
sorting.c:21:19: error: expected expression
students[2] = {"Alief Dean", 22, "Male"};
^
sorting.c:22:19: error: expected expression
students[3] = {"Adwi Lanang", 21, "Male"};
^
sorting.c:23:19: error: expected expression
students[4] = {"Dhimas Kuncahyo", 22, "Male"};
But for the second code, it works well without any error.
Second Code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#define MaxStudents 5
struct student
{
string firstName;
string lastName;
int age;
};
int main(void)
{
struct student kevin = {"Kevin", "Mahendra", 22};
printf("%s %s, %i
", kevin.firstName, kevin.lastName, kevin.age);
}
So what do you guys think the problem on my first code? What is expected expression means?
As you can see i'm just trying to assign value into the array of structures with only write it insdie {} just like i did in second code (But not an array). Please help me. Thank You.
question from:
https://stackoverflow.com/questions/65846035/struct-in-c-assign-value-into-array-of-struct