张传旭 2016-06-08 03:48 采纳率: 92.3%
浏览 1038
已采纳

万能的大神,帮忙看一下,程序运行之后会出现一个中断

![

 #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **create_memory(int num)//分配内存
{
    int i = 0;
    char **p1 = NULL;
    p1 = (char**)malloc(sizeof(char*)*num);
    if (p1 == NULL)
    {
        return NULL;
    }
    for (i = 0; i < num; i++)
    {
        p1[i] = (char*)malloc(sizeof(char) * 100);
    }
    return p1;
}
int count_string(char *str)//判断能分成几个字符串
{
    int count = 0;
    if (str == NULL)
    {
        return -1;
    }
    while (*str != '\0')
    {
        if (*str == ',')
        {
            count++;
        }
        str++;
    }
    return count + 1;
}
int split_string(char *str_old, char **str_new, int *length)//拆分字符串
{
    int ret = 0;
    if (str_old == NULL || str_new == NULL || length == NULL)
    {
        return -1;
    }
    int count = 1;
    int i = 0;
    char *tmp = str_old;
    while (*tmp != '\0')//将逗号全部替换为\0
    {
        if (*tmp == ',')
        {
            *tmp = '\0';
            count++;
        }
        tmp++;
    }
    *length = count;
    for (i = 0; i < count; i++)
    {
        str_new[i] = str_old;

        while (*str_old++ != '\0');
    }

    return ret;
}
void free_all(char **p1, int num)
{
    int i = 0;
    for (i = 0; i < num; i++)
    {
        if (*(p1 + i) != NULL)
        {
            free(*(p1+i));
            *(p1 + i) = NULL;
        }
    }
    if (p1 != NULL)
    {
        free(p1);
    }


}
int printf_all(char **p1, int num)
{
    int ret = 0;
    if (p1 == NULL)
    {
        return -1;
    }
    int i = 0;
    for (i = 0; i < num; i++)
    {
        printf("%s\n", p1[i]);
    }
    return ret;
}
void main()
{
    int ret = 0;
    char str[100] = "abcdef,ghijk,lmno,pqrst,aaaaaa";
    int length = 0;
    int num = 0;

    num = count_string(str);//num=传输出的二级指针行数
    if (num < 0)
    {
        printf("ERROR!!! function:count_string()%d", num);
        return;
    }

    char **p1 = NULL;
    p1 = create_memory(num);//分配内存
    if (p1 == NULL)
    {
        printf("ERROR!!! function:create_memory()");
        return;
    }

    ret = split_string(str, p1, &length);//拆分函数
    if (ret != 0)
    {
        printf("ERROR!!! function:split_string()");
        return;
    }

    printf("%d\n", length);//打印行数

    ret=printf_all(p1, num);// 打印
    if (ret != 0)
    {
        printf("ERROR!!! function:printf_all()");
        return;
    }

    free_all(p1, num);// 释放内存,问题就出在这
    system("pause");
}

图片说明](https://img-ask.csdn.net/upload/201606/08/1465357699_863906.png)

  • 写回答

1条回答 默认 最新

  • 张传旭 2016-06-08 04:31
    关注

    只要把split string 里的strnew[i]=strold改为用strcpy就行了,哈哈,想了好久

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像