gpt4 book ai didi

c - fgets "Access violation writing location 0xCCCCCCCC."错误

转载 作者:行者123 更新时间:2023-12-02 07:33:59 24 4
gpt4 key购买 nike

Error: Unhandled exception at 0x60092A8D (msvcr110d.dll) in C_Son60.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.

当执行以下代码时,会给出此错误代码。(编译成功)我的错误在哪里?

#include <stdio.h>

int i;

int main(void){

char *names[3];

//get the names of the cities
puts("Enter names of cities");
for (i = 0; i < 3; i++)
{
fgets( names[i], 99, stdin);
}
//print entered names
for (i = 0; i < 3; i++)
{
printf("%s", *names[i]);
}

getch();
}

最佳答案

在读入之前需要分配char指针指向的内存

例如:

for (i = 0; i < 3; i++)
{
names[i] = malloc(200);
fgets( names[i], 99, stdin);
}

关于c - fgets "Access violation writing location 0xCCCCCCCC."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18849480/

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