【发布时间】:2020-11-18 20:06:19
【问题描述】:
我正在做一个项目,我必须替换字符串中的一些字符。 我不明白我看到的错误之一。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void replaceLetters(char *text, char original, char new_char);
{
for (int counter = 0; text[counter] != '\0'; counter++)
{
if (text[counter] == original)//Error occurs here
{
text[counter] = new_char;
}
printf("%c", chr[counter]);
}
return 0;
}
int main()
{
char *text = "HallO";
char original = 'O';
char new_char = 'N';
replaceLetters(text, original, new_char);
return 0;
}
if 语句出现以下错误:thread 1 exc_bad_access (code=1 address=0x0)。
这是什么意思,我该如何解决?
【问题讨论】:
-
text指向字符串文字,您不能写入字符串文字。改为char text[] = "HallO"; -
那是 not 你的代码,因为 that 代码不会编译,因为使用了不存在的
chr[]。
标签: c