我正在学习指针,但我不明白指针如何与 C 风格的字符串一起工作。为什么这两个是等价的?
char a[] = "Gme";
char* p = a; //Why am I allowed to assign "Gme" to a pointer (pointer is an address)
cout << p << " " << *(p+1); //Why does it print "gme" with "cout<<p" (I mean, I am printing an address)?
和
char a[] = "Gme";
char* p = &a[0]; // How is this the same as char* p = a;
cout << p << " " << *(p+1);
总的来说,我不明白指针如何与字符串一起工作。字符如何存储在内存中?如果我们将字符串视为字符数组,为什么我不能打印字符元素的地址?
提前致谢 :)