qq_47179051 2021-12-11 16:46 采纳率: 100%
浏览 24
已结题

C语言:创建链表时,内存申请出错

创建链表中,在申请结点的内存时出现了问题
#include <stdio.h>
#include <string.h>

#define MAXSIZE 20//字符串的最大长度
#define T 3//阈值

typedef struct data_splited {//数据包的数据域
    char* s;
    int length;
    char* p;
}Data_splited;
typedef struct list_splited {//将字符串切分后的数据包
    Data_splited data;
    struct list_splited* next;
}List_Splited;
typedef struct//创建函数返回的类型,其返回的字串最多为20个
{
    char S[10][MAXSIZE];
}Result_split;
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/943192212936180.png "#left")


List_Splited* Split(char*);



int main()
{
    List_Splited* result = Split("helloworld");

    return 0;
}


List_Splited* Split(char* str)
{
    if (str == NULL)
    {
        printf("您输入的字符为空!");
        return;
    }
    else
    {
        Result_split result = split(str);
        int length = strlen(str);
        int n = length / T;

        //创建头、尾结点
        List_Splited* temp;
        int i = 1;
        List_Splited* head = NULL;
        List_Splited* tail = NULL;
        temp = (List_Splited*)malloc(sizeof(List_Splited));
        head = temp;
        temp->data.s = NULL;
        //数据输入
        strcpy_s(temp->data.s, 10, result.S[0]);
        temp->data.length = length;
        temp->data.p = str;
        //
        head->next = NULL;
        tail = temp;
        temp = NULL;
        while (i < n)
        {
            temp = (List_Splited*)malloc(sizeof(List_Splited));
            tail->next = temp;
            //数据输入
            strcpy_s(temp->data.s, 10, result.S[i]);
            temp->data.length = length;
            temp->data.p = str;
            //
            temp->next = NULL;
            tail = temp;
            temp = NULL;
            i++;
        }
        return head;
    }
}
申请内存前

img

申请内存后

img

  • 写回答

2条回答 默认 最新

  • 万小橘 2021-12-11 17:17
    关注

    如果是内存分配的错,加个头文件#include

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月19日
  • 已采纳回答 12月11日
  • 修改了问题 12月11日
  • 创建了问题 12月11日

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了