我对字符数组的内存地址感到困惑。以下是演示代码:
char input[100] = "12230 201 50";
const char *s = input;
//what is the difference between s and input?
cout<<"s = "<<s<<endl; //output:12230 201 50
cout<<"*s = "<<*s<<endl; //output: 1
//here I intended to obtain the address for the first element
cout<<"&input[0] = "<<&(input[0])<<endl; //output:12230 201 50
char数组本身就是一个指针吗?为什么“&”操作符不能给出char元素的内存地址?如何获得单个条目的地址?谢谢!
\n
与指针无关,它只是一个换行符。重要的是要打印一个void*
。 - Johannes Overmannstd::endl
输出一个换行符,同时刷新输出缓冲区。手动刷新缓冲区通常不需要,因此许多人更喜欢使用 '\n' 或 "\n"(在某些情况下可能更快)。 - Blastfurnacestd::endl
是一个误解,在正常代码中不应该使用。 - Johannes Overmann