用Strcpy分割故障 [英] segmentation fault with strcpy

查看:23
本文介绍了用Strcpy分割故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我在下面的代码中遇到分段错误。

int main(void)
{
        char str[100]="My name is Vutukuri";
        char *str_old,*str_new;

        str_old=str;
        strcpy(str_new,str_old);

        puts(str_new);

        return 0;
}

推荐答案

您尚未初始化*str_new,因此它只是将str_old复制到某个随机地址。 您需要执行以下任一操作:

char str_new[100];

char * str = (char *) malloc(100);

如果您在使用malloc函数时尚未使用#include <stdlib.h>,则必须#include <stdlib.h>

这篇关于用Strcpy分割故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆