Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

Struct in C (assign value into array of struct)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
", students[i].name, students[i].age, students[i].gender);
    }
}

https://godbolt.org/z/KvE7G1


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...