Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struct in C (assign value into array of struct)

Tags:

c

struct

cs50

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\n", 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.


1 Answers

There is a difference between initialization (second code) and the assignment (first code)

This syntax is only valid if you initialize the object, not when you assign. As structs are copied when assigned you can use compound literals for that:

#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] = (student){"Kevin Mahendra", 22, "Male"};
    students[1] = (student){"Wasis Sirutama", 22, "Male"};
    students[2] = (student){"Alief Dean", 22, "Male"};
    students[3] = (student){"Adwi Lanang", 21, "Male"};
    students[4] = (student){"Dhimas Kuncahyo", 22, "Male"};

    for (int i = 0; i < MaxStudents; i++)
    {
        printf("%s, %i %s\n", students[i].name, students[i].age, students[i].gender);
    }
}

https://godbolt.org/z/KvE7G1

like image 68
0___________ Avatar answered Dec 03 '25 18:12

0___________



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!