咕噜~灵波~ 2022-12-06 18:10 采纳率: 25%
浏览 12

显示未初始head内存


//创建一个单链表
struct Node {
    int data;//4
    struct Node* next;//4
};

struct Node* create_link(struct Node* head)
{
    head = (struct Node *)malloc(sizeof(struct Node));//8
    head->next = NULL;

    struct Node* tail = head;

    for (int i = 1; i <= 10; i++)
    {
        struct Node* temp = head = (struct Node*)malloc(sizeof(struct Node));//分配一块内存空间
        temp->data = i;
        temp->next = NULL;
        tail->next = temp;
        tail = temp;
    }

    return head;
}

void print_link(struct Node* head)
{
    //不带头节点
    head = head->next;
    while (head != NULL) {
        printf("%d\n", head->data);
        head = head->next;
    }
}

int main() {
    struct Node *head;
    head = create_link(head);
    print_link(head);
    return 0;
}

```结果显示没初始化head内存

严重性    代码    说明    项目    文件    行    禁止显示状态
错误    C4700    使用了未初始化的局部变量“head”
  • 写回答

2条回答 默认 最新

  • laocooon523857886 2022-12-06 23:58
    关注
    
    
    ```c
    #include<stdio.h>
    #include<stdlib.h>
    //创建一个单链表
    struct Node {
        int data;//4
        struct Node* next;//4
    };
    struct Node* create_link(struct Node* head)
    {
        head = (struct Node *)malloc(sizeof(struct Node));//8
        head->next = NULL;
        struct Node* tail = head;
        for (int i = 1; i <= 10; i++)
        {
            struct Node* temp = (struct Node*)malloc(sizeof(struct Node));//分配一块内存空间
            temp->data = i;
            temp->next = NULL;
            tail->next = temp;
            tail = temp;
        }
        return head;
    }
    void print_link(struct Node* head)
    {
        //不带头节点
        head = head->next;
        while (head != NULL) {
            printf("%d\n", head->data);
            head = head->next;
        }
    }
    int main() {
        struct Node *head;
        head = create_link(head);
        print_link(head);
        return 0;
    }
    
    

    ```

    评论

报告相同问题?

问题事件

  • 创建了问题 12月6日

悬赏问题

  • ¥100 复现论文:matlab仿真代码编写
  • ¥15 esp32驱动GC9A01循环播放视频
  • ¥15 惠普360g9的最新bios
  • ¥30 这个功能用什么软件发合适?
  • ¥60 微信小程序,取消订单,偶尔订单没有改变状态
  • ¥15 用pytorch实现PPO算法
  • ¥15 关于调制信号的星座图?
  • ¥30 前端传参时,后端接收不到参数
  • ¥15 这是有什么问题吗,我检查许可证了但是显示有呢
  • ¥15 机器学习预测遇到的目标函数问题