【问题标题】:Using loop to create new struct in C使用循环在 C 中创建新结构
【发布时间】:2020-10-09 03:22:58
【问题描述】:

在这里,我创建了一个以姓名、卷号、分数等为成员的学生结构。所以,我想做的是每次迭代循环时创建一个新结构,并在循环时将数据保存在其中。

所以,我创建了一个变量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 
    }
}

【问题讨论】:

  • 请更清楚地描述问题。 “它不起作用” 毫无帮助。我可以立即看到的一个问题是,您使用相同标识符的结构重新定义了名为 stdnamechar 数组。 C 语言不允许这样做。编译器应该已经发布了一条引用该代码行的错误消息,并带有一条类似于“重新定义”的消息。它试图指出错误,以便您修复它。
  • 如果这是一个练习或家庭作业,请edit你的帖子包含完整的描述。无论如何,您需要告诉我们您认为该程序有什么问题。发布您对其进行测试的输入、预期输出和实际结果。
  • 总是在编译时启用警告,并且接受代码,直到它编译时没有警告。要启用警告,请将 -Wall -Wextra -pedantic 添加到您的 gcc/clang 编译字符串(也可以考虑添加 -Wshadow 以警告阴影变量)。对于 VS(Windows 上的cl.exe),使用/W3。所有其他编译器将具有类似的选项。阅读并理解每个警告——然后去修复它。他们将识别任何问题,以及它们发生的确切位置。
  • "24:24: error: conflicting types for ‘stdname’"
  • 你不能用两种不同的类型两次声明同一个变量名。

标签: c loops struct


【解决方案1】:

我建议您通过实施链表或标签系统来解决您的问题。我个人更喜欢链表,因为当你的代码进化时,在我的 pov 中使用链表会更容易。

所以在代码中你会在你的结构中看到我添加了一个next指针这个指针将存储另一个学生的地址,如果下一个是NULL那么我们可以确认没有更多的学生并且我们可以停止爬行通过列表。

函数添加使用此原理;她走到列表的末尾,然后将next 替换为新学生结构的地址。并且因为新结构的下一个是NULL这个过程它可以重复使用

函数create_new_node只是数据初始化和内存分配。 它看起来很丑,因为我真的不知道你想要存储什么。

我没有测试这段代码,它根本没有优化

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

这是我的命题, 请注意我是否笨拙,我是堆栈上的新用户。

也可以随时提出问题或修正我自己的错误

希望能帮到你?

【讨论】:

    猜你喜欢
    • 2019-04-14
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多