gpt4 book ai didi

c - 无法使用 MINGW 执行 C 程序

转载 作者:行者123 更新时间:2023-11-30 18:29:31 28 4
gpt4 key购买 nike

我无法通过 mingW 开发环境执行哪怕是简单的 C 代码。这段代码工作正常

#include<stdio.h>
int main(){
char ans[5];
printf("Enter yes or no");
scanf("%s", ans);
printf("You just entered", ans);
return 0;
}

但是每当我将ans的数据类型转换为char*,然后执行命令创建的.exe文件

gcc basic.c -o basic.exe

我看不到输出,它只是说basic.exe 已停止工作。不知道是mingW安装有问题还是什么。

最佳答案

您不应该看到输出,程序应该崩溃,因为将 ans 改为 char* 还不够,还需要使用 malloc 为字符串分配位置:

ans=malloc(sizeof(char) * 5);

而且 printf 应该是:

printf("....%s",ans);

关于c - 无法使用 MINGW 执行 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37772390/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com