数组、指针、字符串与内存

  • string a = "helloworld"

    在内存的rodata区(只读数据段)中

  • char *p = "helloworld";
    
  1. p在栈中,只分配1个字节,并且没有拷贝"helloworld"的功能,所以"helloworld"仍在rodata中
  2. 若此时再写*p=H;则表示修改rotate中的"helloworld",会有段错误出现
  • char a[]="helloworld";
    a[0]='H';
  1. 第一句话,用在rotate中"helloworld"初始化数组a,此时a在栈空间中,并且拷贝了"helloworld"在栈空间中,再执行第二句话,修改的是在栈空间的被拷贝的"helloworld",并不是在rotate中"helloworld",不会发生段错误,而将"helloworld"改成"Helloworld"
  2. 而之前的在rotate中的"helloworld"只是在初始化时使用,之后再无用处

猜你喜欢

转载自blog.csdn.net/qq_42420263/article/details/86582192
今日推荐