当我编译此代码时,我遇到了总线错误,并且我不明白我正在做什么导致此错误。我阅读了总线错误所涉及的内容,但我仍然不太明白我的问题。此代码应重现 string.h 库中 strcpy() 函数的行为。
#include <unistd.h>
void ft_putstr(char *str)
{
while (*str)
write(1, str++, 1);
}
char *ft_strcpy(char *dest, char *src)
{
int index;
index = 0;
while (src[index] != '\0')
{
dest[index] = src[index];
index++;
}
dest[index] = '\0';
return (dest);
}
int main(void)
{
char *str1, *str2, *cpy;
str1 = "Cameron";
str2 = "Why you no work?";
cpy = ft_strcpy(str1, str2);
ft_putstr(cpy);
return (0);
}
最佳答案
char *name = "string";
在内存中创建一个字符串文字,以后无法重写。 使用 malloc 和您的函数而不是像这样的初始化。
关于c - 我在哪里得到这个总线错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49854638/