c - 我无法解决这个问题。当我编译时,编译器因为 free func 给出了一个错误

标签 c

#include <stdio.h>
#include <stdlib.h>


void copyint(char *i,char** temp);
int main()
{
    char* a="00313";
    char* temp;
    int inte;
    temp=(char*)malloc(sizeof(char));

    copyint(a,&temp);

    inte=atoi(temp);
    printf("%s\n",temp);
    system("PAUSE");
    free(temp);
}

void copyint(char *i,char** temp)
{
    *temp=i;
}

最佳答案

没问题,所以第一眼就指出问题所在: - copyint 将单个字符复制到指向的内存中。 那么正在做什么:临时值为“0”,随机,随机......

'0',随机 random 由 atoi 解析 - 未定义,因为我们不知道该指针下的内容。 然后打印出来...

使用strlen malloc strcpy序列代替

编辑:它不会编译,因为您将 (char*) 传递到接受 (char) 的函数中。 (copyint(a,&temp);)

关于c - 我无法解决这个问题。当我编译时,编译器因为 free func 给出了一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4645502/

相关文章:

c - 函数指针的缓冲区?

c - 应用程序日志级别

sizeof() 是否可以为同一类型返回不同的值?

C编程: Delete specific value stored in Array

c - Zlib 内核模块

c - C 中列表的第一个元素不正确(linux/list.h)

c - 向 2 个客户端发送 UDP 数据包,只有一个接收

c - 变量与存储在数组中的值的高效匹配

C初学编程求助日历代码更新

c - 如何使用 SV-DPI 将数组从 C 传递到 SV?