Segmentation fault怎么解决
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/16 01:51:32
Segmentation fault怎么解决
Segmentation fault怎么解决
Segmentation fault怎么解决
#include
#include
int main(void)
{
\x09char s[25]="####";
\x09char d[256]="This is a test for memcpy";
\x09char *ptr = NULL;
\x09printf("destination before memcpy: %s\n",d);
\x09ptr=memcpy(d,s,strlen(s));
\x09if(ptr)
\x09\x09printf("destination after memcpy: %s\n",d);
\x09else
\x09\x09printf("memcpy failed\n");
\x09return 0;
}
数组对应着一块内存区域,而指针是指向一块内存区域.其地址和容量在生命期里不会改变,只有数组的内容可以改变;而指针却不同,它指向的内存区域的大小可以随时改变,而且当指针指向常量字符串时,它的内容是不可以被修改的,否则在运行时会报错.
MEMCPY(3) Linux Programmer鈙 Manual MEMCPY(3)
NAME
memcpy - copy memory area
SYNOPSIS
#include
void *memcpy(void *dest, const void *src, size_t n);
DESCRIPTION
The memcpy() function copies n bytes from memory area src to memory
area dest. The memory areas should not overlap. Use memmove(3) if the
memory areas do overlap.