Segmentation fault around strcpy?

前端 未结 4 1932
情书的邮戳
情书的邮戳 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-10 00:06

    i'm just wondering why this segmentation fault.

    Because if cmd is a global variable, its value is NULL, which is not writable, and if it's a local variable, then its value is indeterminate and you should not use it (but it can do anything if you do, which is worse than NULL in many cases).

提交回复
热议问题