簡體   English   中英

C 中的結構體(將值賦給結構體數組)

[英]Struct in C (assign value into array of struct)

我在 C 中有這兩個不同的代碼。 他們都只是試圖將價值分配給結構。 我正在使用 cs50 庫來幫助我以簡單的方式獲取字符串。

對於第一個代碼,當我嘗試執行它時出現錯誤。

第一個代碼:

#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);
    }

}

問題(第一個代碼):

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"};

但是對於第二個代碼,它運行良好,沒有任何錯誤。

第二個代碼:

#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);
}

那么你們認為我的第一個代碼有什么問題? 預期表達是什么意思?

如您所見,我只是想將值分配給結構數組,就像我在第二個代碼中所做的那樣(但不是數組),只將其寫入 insdie {}。 請幫我。 謝謝你。

問題是初始化和賦值之間的區別。 初始化基本上是與聲明一起完成的賦值。

當您想要分配一個復合文字(例如{"Kevin Mahendra", 22, "Male"} )時,您需要通過將(student)放在它前面來進行轉換,但在初始化期間不需要轉換。

注意:從技術上講,它不是演員表。 但它看起來確實像演員表,許多人稱它為演員表,要么是因為缺乏知識,要么只是因為語言草率。 在 Eric Postpischil 指出之前,我認為這是一個演員表。

改成

students[0] = (student) {"Kevin Mahendra", 22, "Male"};

你也可以這樣做:

student students[MaxStudents] = {
    {"Kevin Mahendra", 22, "Male"},
    {"Wasis Sirutama", 22, "Male"},
    {"Alief Dean", 22, "Male"},
    {"Adwi Lanang", 21, "Male"},
    {"Dhimas Kuncahyo", 22, "Male"},
};

請注意,您可以通過這種方式初始化 arrays,就像使用結構一樣。 但是,以后不能以這種方式分配數組。 拿這個代碼:

int arr[3];
arr = // Will not work no matter what you type here.

arr =之后你絕對沒有任何東西可以編譯。 您可以嘗試arr = {1,2,3}arr = (int[3]) {1,2,3}但它不起作用,因為在任何情況下都不允許分配給數組。 但是,如果將數組包裝在結構中,則可以使用解決方法。

初始化(第二個代碼)和賦值(第一個代碼)是有區別的

此語法僅在您初始化 object 時有效,在分配時無效。 由於在分配時會復制結構,因此您可以使用復合文字:

#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

第二段代碼是初始化:

struct student kevin = {"Kevin", "Mahendra", 22};

在這里,您定義一個變量並提供一個初始值。 這里=右邊的部分被解析為struct student類型。

在第一個代碼中只有一個賦值

students[0] = {"Kevin Mahendra", 22, "Male"};

並且在賦值中,分配給的變量的類型不用於右側的評估。 所以編譯器無法知道{"Kevin Mahendra", 22, "Male"}應該是什么。

這可以通過使用復合文字(本質上看起來像強制轉換)來解決:

students[0] = (student){"Kevin Mahendra", 22, "Male"};

或者您可以在定義students數組時對其進行初始化:

student students[MaxStudents] = {
    {"Kevin Mahendra", 22, "Male"},
    {"Wasis Sirutama", 22, "Male"},
    {"Alief Dean", 22, "Male"},
    {"Adwi Lanang", 21, "Male"},
    {"Dhimas Kuncahyo", 22, "Male"},
};

您只能在創建時初始化這樣的struct struct student kevin = {"Kevin", "Mahendra", 22}; 會工作得很好,但試試struct student kevin; kevin = {"Kevin", "Mahendra", 22}; struct student kevin; kevin = {"Kevin", "Mahendra", 22}; ; 它會給出一個錯誤。

創建數組時,它會在每個索引處放置一個未初始化的結構,這意味着您無法在之后對其進行初始化。 [編輯] 正如 klutt 提到的,您可以使用{}設置值並進行強制轉換。

為了完整起見,丑陋的:

#include <string.h>

  ...

  student students[MaxStudents];

  memcpy(students, 
    (student[MaxStudents])
    {
      {"Kevin Mahendra", 22, "Male"},
      {"Wasis Sirutama", 22, "Male"},
      {"Alief Dean", 22, "Male"},
      {"Adwi Lanang", 21, "Male"},
      {"Dhimas Kuncahyo", 22, "Male"},
    },
    sizeof (student[MaxStudents])
  );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM