can't modify char* - Memory access violation

前端 未结 4 1904
梦如初夏
梦如初夏 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:11

    To fix this, use an array instead of a pointer to read-only memory:

    char str[] = "HelloGuys";   // change this line
    int len = strlen(str);
    for (int i=0; i<(len/2); ++i){
        char t = str[len-i-1];
        str[len-i-1] = str[i];
        str[i] = t;
    }
    

提交回复
热议问题