Segmentation fault around strcpy?

前端 未结 4 1928
情书的邮戳
情书的邮戳 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:59

    You get a seg. fault because cmd in your first example isn't pointing to anything (or, rather, it's pointing to something that's undefined - so attempting to read characters from or write characters to the pointer will probably result in an access violation).

    In the second example, you're setting cmd to point to a legitimate string of chars.

提交回复
热议问题