代码应该将每个单词的每个字母转换为大写。但运行代码会导致总线错误。是什么原因导致总线错误?
#include <stdio.h>
char *ft_strupcase(char *str)
{
int index;
index = 0;
while (str[index] != '\0')
{
if (str[index] >= 97 && str[index] <= 122)
str[index] = 65 + str[index] - 97;
index++;
}
return (str);
}
int main()
{
char *name = "sEbas";
printf("%s\n", ft_strupcase(name));
return (0);
}
输入:sEbas
输出:SEBAS
最佳答案
字符串“sEbas”是一个const char[]
,因此您不应该使用它来初始化char* name
。
如果您在 Visual Studio 2019 上编译此代码,它将无法编译并会告诉您错误:
无法从“const char [6]”转换为“char *”
您可以将其替换为:
字符名称[] = "sEbas";
关于c - 该代码应该将每个单词的每个字母转换为大写。但运行代码会导致总线错误。总线错误的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58127175/