can't modify char* - Memory access violation

前端 未结 4 1903
梦如初夏
梦如初夏 2020-11-30 10:26

Why does it say \"Memory access violation\"?

  char* str = \"HelloGuys\";
  int len = strlen(str);
  for (int i=0; i<(len/2); ++i){
        char t = str[l         


        
4条回答
  •  醉梦人生
    2020-11-30 11:22

    String literals are stored in read only section of memory. Any attempt to modify the contents of a string literal invokes Undefined Behaviour (segmentation fault on most implementations).

    Use an array of characters rather

    char str[] = "HelloGuys";
    

提交回复
热议问题