问题描述:

This question already has an answer here:

  • Segmentation fault of char * assignment [duplicate]

    4 answers

网友答案:

The string "t" is put into read only memory. For historical reasons you can use char *. Instead do

char g[] = "t";

This will create a read/write copy on the stack.

网友答案:

Because g is string literal and you are trying to modify it inside function helper

char *g = "t";  // <-- this is string literal

Change this to array syntax, and you're ok

char g[] = "t";
相关阅读:
Top