Segmentation fault around strcpy?

前端 未结 4 1930
情书的邮戳
情书的邮戳 2020-12-09 23:47

I know that you will rap me over the knuckles but.

Why does it make Segmentation fault

char* cmd;
strcpy(cmd, argv[0]);

when this d

4条回答
  •  隐瞒了意图╮
    2020-12-09 23:55

    If you want to make copy of argv[0] easily,

    char* cmd = strdup(argv[0]);
    

    Of course, you have better to check result of strdup is null or not. :)

提交回复
热议问题