gpt4 book ai didi

c - 如何修复访问冲突写入异常

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

我有以下代码

#include <stdio.h>
#include<conio.h>
#include <ctype.h>
char *convertire(char *sir)
{
char *sir2="";
while (*sir)
{
*sir2 = toupper(*sir);
sir++;
}
return (sir2);
}
void main(void)
{
char *sir = "Home is good";
char *sir2 = convertire(sir);
printf("Sirul obtinut este : %s\n", sir2);
_getch();
}

当我尝试编译时出现以下错误

First-chance exception at 0x00E3141B in pointeri.exe: 0xC0000005: Access violation writing location 0x00E35858. If there is a handler for this exception, the program may be safely continued.

你能帮我解决这个问题吗?

最佳答案

您还需要跟踪 sir2 的开始,基本上您需要像这样修改 convertire():

char *convertire(char *sir)
{
char *p;
char *sir2=(char *)malloc(strlen(sir)+1);
p = sir2;
while (*sir)
{
*sir2++ = toupper(*sir++);
}
return p;
}

关于c - 如何修复访问冲突写入异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29624933/

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