c - 我在哪里得到这个总线错误?

标签 c string compiler-errors

当我编译此代码时,我遇到了总线错误,并且我不明白我正在做什么导致此错误。我阅读了总线错误所涉及的内容,但我仍然不太明白我的问题。此代码应重现 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/

相关文章:

c++ - C 套接字发送/接收缓冲区类型

c - Realloc和一个函数还是malloc和两个函数?

c - 一旦可用就读取子进程的输出?

string - 如何让 bash "eat"字符串中所有行共有的缩进字符?

compiler-errors - 如何在Ubuntu 17.10上编译WordNet-3.0?

c++ - 复制构造函数错误 : returning a value from a constructor

c - 我必须为 Posix 计时器使用信号处理程序吗?

使用 C 编程使用 argc 和 argv 参数创建一个刽子手游戏程序

c++ - 如何使用libc++ libstdc++混合编译的链接库

c++ - 空字符常量