簡體   English   中英

使用循環在 C 中創建新結構

[英]Using loop to create new struct in C

在這里,我創建了一個學生結構,其成員包括姓名、學號、分數等。 所以,我想做的是在每次迭代循環時創建一個新結構,並在循環時將數據保存在其中。

因此,我創建了一個變量stdname ,每次迭代都會更改它的值,並將其用作命名新學生的方式,但它不起作用。 我是 C 的新手,我不知道我的代碼有什么問題。

#include <stdio.h>

struct Marks

{
    int phy;
    int chem;
    int math;
};

struct Student{
    char name[50];
    int rollN0;
    char remarks[100];
    struct Marks marks;
};

int main()
{
    for (int i=0; i<10; i++)
    {
        char stdname[50];
        sprintf(stdname,"student%d",i+1);
        struct Student stdname;
        printf("Enter the following data of Student No. %d:\n", i+1); 
        //taking data from user and storing 
    }
}

我建議您通過實施鏈接列表或選項卡系統來解決您的問題。 我個人更喜歡鏈表,因為當你的代碼進化時,在我的 pov 中使用列表會更容易。

所以在代碼中你會在你的結構中看到我添加了一個next指針,這個指針將存儲另一個學生的地址,如果下一個是NULL那么我們可以確認沒有更多的學生並且我們可以停止在列表中爬行。

function 添加使用此原則; 她轉到列表的末尾,然后用新學生結構的地址替換next 並且因為下一個新結構是NULL這個過程它可以重復使用

function create_new_node只是數據初始化和memory分配。 它看起來非常難看,因為我真的不知道你想要存儲什么。

我沒有測試這段代碼,它根本沒有優化

#include <stdio.h>
#include <stdlib.h> // for malloc function

struct Marks

{
    int phy;
    int chem;
    int math;
};

struct Student{
    char name[50];
    int rollN0;
    char remarks[100];
    struct Marks marks;
    struct Student* next;  // I had this
};
/// new ///
void add_node_to_list(struct Student* node, struct Student* list)
{
    while (list->next != NULL)
        list = list->next;
    list->next = node;
}

struct Student* create_new_node(struct Student data)
{
    struct Student* new_node = malloc(sizeof(struct Marks));

    if (!new_node)
        return NULL;

    new_node->name = data.name;
    new_node->rollN0 = data.rollN0;
    new_node->remarks = data.remarks;
    new_node->marks = data.marks;
    new_node->next = NULL;

    return new_node;
}
/// end-new ///

int main(void /* new */)
{
    struct Student* list = NULL;
    struct Student* new_node = NULL;

    for (int i=0; i<10; i++)
    {
        char stdname[50];
        sprintf(stdname,"student%d",i+1);
        /// new ///
        if (!list) {                        // first the first element
            list = create_new_node((struct Marks) {stdname, 0, "\0", {0, 0, 0}, NULL}); 
            if (!list)        // check for malloc errors
                return 1;
        } else {                            // every time there is a new student
            new_node = create_new_node((struct Marks) {stdname, 0, "\0", {0, 0, 0}, NULL});
            if (!new_node)  // check for malloc errors
                return 1;
            add_node_to_list(new_node, list);
        }
        /// end-new ///
        printf("Enter the following data of Student No. %d:\n", i+1);
        //taking data from user and storing
    }
    return 0; // new: your main should always return a value. Compile with W flags see: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
}

這是我的建議,如果我笨拙的話請注意我,我是堆棧上的新用戶。

也可以隨意提問或修復我自己的錯誤

希望我幫助了你

暫無
暫無

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

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