Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 线程1执行错误访问(代码=1地址=0x0)_C - Fatal编程技术网

C 线程1执行错误访问(代码=1地址=0x0)

C 线程1执行错误访问(代码=1地址=0x0),c,C,我正在做一个项目,我必须替换字符串中的一些字符。 我不明白我看到的其中一个错误 #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++) { i

我正在做一个项目,我必须替换字符串中的一些字符。 我不明白我看到的其中一个错误

#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;
}
#包括
#包括
#包括
作废替换字母(字符*文本、字符原件、字符新字符);
{
对于(int counter=0;text[counter]!='\0';counter++)
{
if(text[counter]==original)//此处发生错误
{
文本[计数器]=新字符;
}
printf(“%c”,chr[计数器]);
}
返回0;
}
int main()
{
char*text=“你好”;
char original='O';
char new_char='N';
替换字母(文本、原件、新字符);
返回0;
}
if
语句中出现以下错误:
线程1 exc\u bad\u访问(code=1 address=0x0)

这意味着什么?我如何解决它?

在c语言中,像“HallO”这样的字符串文本存储在全局只读内存中。如果要修改字符串,需要将其保存在堆栈的缓冲区中

  char text[6] = "HallO";

在c语言中,像“HallO”这样的字符串文本存储在全局只读内存中。如果要修改字符串,需要将其保存在堆栈的缓冲区中

  char text[6] = "HallO";
“这意味着什么?我该如何解决?”

这是一种访问冲突。您定义的字符串

char *text = "HallO";  
在C中称为a,并在只读内存的区域中创建,导致访问冲突

通过创建可编辑的原始变量,可以轻松解决这一问题。例如:

char text[6] = "HallO"; //okay
char text[] = "HallO"; //better, let the compiler do the computation
char text[100] = "HallO"; //useful if you know changes to string will require more room
“这意味着什么?我该如何解决?”

这是一种访问冲突。您定义的字符串

char *text = "HallO";  
在C中称为a,并在只读内存的区域中创建,导致访问冲突

通过创建可编辑的原始变量,可以轻松解决这一问题。例如:

char text[6] = "HallO"; //okay
char text[] = "HallO"; //better, let the compiler do the computation
char text[100] = "HallO"; //useful if you know changes to string will require more room

text
指向字符串文本,不能写入字符串文本。更改为
char text[]=“你好”这是否回答了您的问题?这不是您的代码,因为由于使用了不存在的
chr[]
text
指向字符串文本,因此该代码将无法编译,您无法写入字符串文本。更改为
char text[]=“你好”这是否回答了您的问题?这不是您的代码,因为由于使用了不存在的
chr[]
,该代码将无法编译。作为旁白,您通常最好使用
char text[]=…
这样您就不必计算大小。作为旁白,您通常最好使用
char text[]=…
这样您就不必计算大小。