简体   繁体   English

使用循环在 C 中创建新结构

[英]Using loop to create new struct in C

Here, I have created a structure of student having name, roll no, marks, etc as their members.在这里,我创建了一个学生结构,其成员包括姓名、学号、分数等。 So, what I wanted to do was create a new struct every time it iterates through a loop and saving data in it as it loops through.所以,我想做的是在每次迭代循环时创建一个新结构,并在循环时将数据保存在其中。

So, I created a variable stdname that changes it's value every iteration and use it as a way to name a new student but it's not working.因此,我创建了一个变量stdname ,每次迭代都会更改它的值,并将其用作命名新学生的方式,但它不起作用。 I am fairly new to C and I don't know what is wrong with my code here.我是 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 
    }
}

I propose you to solve your problem by implementing a systeme of linked list or tabs.我建议您通过实施链接列表或选项卡系统来解决您的问题。 I personnaly perfere linked list because when your code will evolute it's going to be easier to used the list in my pov.我个人更喜欢链表,因为当你的代码进化时,在我的 pov 中使用列表会更容易。

So in the code you will see in your structure i add a next pointer this one will store the adresse of another student, if next is NULL then we can affirm there is no more student and we can stop crawling throught the list.所以在代码中你会在你的结构中看到我添加了一个next指针,这个指针将存储另一个学生的地址,如果下一个是NULL那么我们可以确认没有更多的学生并且我们可以停止在列表中爬行。

the function add use this principe; function 添加使用此原则; she goes to the tail of the list then replace the next with the adresse of the new student structure.她转到列表的末尾,然后用新学生结构的地址替换next And because the next of the new structure is NULL this process it re-usable并且因为下一个新结构是NULL这个过程它可以重复使用

The function create_new_node is only data initation and memory allocation. function create_new_node只是数据初始化和memory分配。 It looks very ugly because i don't realy know what you want store.它看起来非常难看,因为我真的不知道你想要存储什么。

I didn't test this code and it's not optimised at all 我没有测试这段代码,它根本没有优化

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

Here is my propostion, Notice me if i had been clumsy, i'm a new user on stack.这是我的建议,如果我笨拙的话请注意我,我是堆栈上的新用户。

Also feel free to ask question or fix my own error也可以随意提问或修复我自己的错误

Hope I helped you希望我帮助了你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM