为什么我不能从C中的函数传回链表?

科尔·达普里奇

我正在尝试通过将指针传递到列表的开头来在函数内部创建链接列表。在函数内部,一切正常。但是,当我回到时main(),指针突然变为NULL。因此,如果我再次调用该函数,则它的行为就像是我第一次添加节点一样。

我的代码有什么问题?

struct course
{
    int c_ID;
    char *c_name;
    struct course *c_next;
};

void new_course(struct course *c_head, struct course *c_tail); // adds a node

int main ( )
{
    // variable declarations
    int choice;
    char y_n;

    // create linked lists
    struct course *c_head = NULL;
    struct course *c_tail = NULL;

    // print out menu, obtain choice, call appropriate function; loop if desired
    do
    {
        printf("\t\t\t***MENU***\n"
               " 1. Add a new course\n\n"
               ................................
               "Enter the number of the menu option you wish to choose: ");
        scanf("%d", &choice);

        switch (choice)
        {
            case 1:
                new_course(c_head, c_tail);
                if (c_tail == NULL)
                {
                    printf("We're screwed.\n"); // this excecutes every time
                }
                break;
            .....................
        }

        printf("Would you like to return to the main menu? Enter y for yes, n for no: ");
        scanf(" %c", &y_n);

    } while (y_n != 'n' && y_n != 'N');

    // free courses
    struct course *c_temp = NULL;
    c_temp = c_head;
    while (c_temp != NULL)
    {
        c_head = c_head->c_next;
        c_temp->c_ID = 0;     // reinitialize the student ID
        c_temp->c_name[0] = '\0'; // reinitialize the student name string
        free(c_temp->c_name);     // return the string memory to the system
        free(c_temp);         // return the node memory to the system
        c_temp = c_head;      // set temp to next item in the list
    }

    return 0;
}

void new_course(struct course *c_head, struct course *c_tail)
{
    // declare variables
    int ID;
    char name[50];

    // obtain user input
    printf("Enter the course ID number and the course name, separated by a space: ");
    scanf("%d%s", &ID, name);

    if(c_head == NULL) // no courses yet
    {
        c_head = (struct course *) malloc(sizeof(struct course)); // allocate memory for c_head
        c_head->c_next = NULL;
        c_tail = c_head; // update c_tail
    }
    else // the list already has nodes
    {
        c_tail->c_next = (struct course *) malloc(sizeof(struct course)); // allocate memory for new node
        c_tail = c_tail->c_next; // update c_tail
        c_tail->c_next = NULL;
    }

    c_tail->c_ID = ID; // assign ID to c_ID component of new node
    c_tail->c_name = (char *) malloc(sizeof(char) * strlen(name) + 1); // allocate memory for c_name component of new node
    strcpy(c_tail->c_name, name); // assign name to c_name component of new node
    printf("%d = %d, %s = %s\n", c_head->c_ID, ID, c_tail->c_name, name); // this always works, proving the list was created and the assignments worked

    return;
}
LoztInSpace

您需要将指针传递给指针,以便可以更改c_head和c_tail的值。

像这样打电话

new_course(&c_head, &c_tail);

像这样使用

void new_course(struct course **c_head, struct course **c_tail)
{

    if((*c_head) == NULL) // no courses yet
    {
        (*c_head) = (struct course *) malloc(sizeof(struct course)); 

... ETC。ETC。

}

我自己不会这样写,但这是您的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我不能从C#中的Powershell.Invoke()方法返回所有Cimsession函数

来自分类Dev

为什么我不能从此模板函数返回引用?

来自分类Dev

为什么我不能从以下程序中获得最小值(使用函数和数组)

来自分类Dev

为什么我不能从函数中更改env.hosts?

来自分类Dev

为什么C#编译器不能从函数签名中推断泛型委托?

来自分类Dev

为什么我不能从远程分支中拉出?

来自分类Dev

为什么我不能从异步代码中捕获异常?

来自分类Dev

我为什么不能从情节提要中创建IBAction

来自分类Dev

为什么我不能从列表中删除所需的元素

来自分类Dev

为什么我不能从该表中删除记录?

来自分类Dev

为什么我不能从XPath查询中检索URL?

来自分类Dev

为什么我不能从远程分支中拉出?

来自分类Dev

为什么我不能从开关内的变量中减去?

来自分类Dev

为什么我不能捕获C ++中的递归lambda函数?

来自分类Dev

为什么我不能从另一个.js文件访问.js文件中定义的JavaScript函数?

来自分类Dev

为什么我们不能从派生类中调用受保护的析构函数?

来自分类Dev

为什么我不能用链表::新?

来自分类Dev

为什么我不能从同一类C#中的方法访问公共字符串

来自分类Dev

为什么我不能从该函数返回通用类型T?

来自分类Dev

为什么我不能从 python 中的另一个函数调用一个函数。函数未定义错误

来自分类Dev

为什么我的javascript window.open函数不能从数组中读取URL,而不能读取宽度和高度参数?

来自分类Dev

为什么不能从函数返回打印函数?

来自分类Dev

为什么我不能从我的活动中调用此服务?

来自分类Dev

为什么我不能从我的Json文件中检索数据?

来自分类Dev

为什么我不能从 sqlite 数据库中检索到我期望的数据?

来自分类Dev

为什么我不能从我用泛型创建的类中调用方法?爪哇

来自分类Dev

为什么我不能从我用泛型创建的类中调用方法?爪哇

来自分类Dev

为什么我不能从我的 sqlite3 数据库中删除?

来自分类Dev

为什么我不能从我的SSD引导?

Related 相关文章

  1. 1

    为什么我不能从C#中的Powershell.Invoke()方法返回所有Cimsession函数

  2. 2

    为什么我不能从此模板函数返回引用?

  3. 3

    为什么我不能从以下程序中获得最小值(使用函数和数组)

  4. 4

    为什么我不能从函数中更改env.hosts?

  5. 5

    为什么C#编译器不能从函数签名中推断泛型委托?

  6. 6

    为什么我不能从远程分支中拉出?

  7. 7

    为什么我不能从异步代码中捕获异常?

  8. 8

    我为什么不能从情节提要中创建IBAction

  9. 9

    为什么我不能从列表中删除所需的元素

  10. 10

    为什么我不能从该表中删除记录?

  11. 11

    为什么我不能从XPath查询中检索URL?

  12. 12

    为什么我不能从远程分支中拉出?

  13. 13

    为什么我不能从开关内的变量中减去?

  14. 14

    为什么我不能捕获C ++中的递归lambda函数?

  15. 15

    为什么我不能从另一个.js文件访问.js文件中定义的JavaScript函数?

  16. 16

    为什么我们不能从派生类中调用受保护的析构函数?

  17. 17

    为什么我不能用链表::新?

  18. 18

    为什么我不能从同一类C#中的方法访问公共字符串

  19. 19

    为什么我不能从该函数返回通用类型T?

  20. 20

    为什么我不能从 python 中的另一个函数调用一个函数。函数未定义错误

  21. 21

    为什么我的javascript window.open函数不能从数组中读取URL,而不能读取宽度和高度参数?

  22. 22

    为什么不能从函数返回打印函数?

  23. 23

    为什么我不能从我的活动中调用此服务?

  24. 24

    为什么我不能从我的Json文件中检索数据?

  25. 25

    为什么我不能从 sqlite 数据库中检索到我期望的数据?

  26. 26

    为什么我不能从我用泛型创建的类中调用方法?爪哇

  27. 27

    为什么我不能从我用泛型创建的类中调用方法?爪哇

  28. 28

    为什么我不能从我的 sqlite3 数据库中删除?

  29. 29

    为什么我不能从我的SSD引导?

热门标签

归档