char * - 为什么指针中没有地址? [英] char* - Why is there no address in the pointer?
问题描述
我有一个关于 char *
的基本问题我不明白
I have a basic question about char*
I dont understand
char* aString = "Hello Stackoverflow";
指针指向字符链的第一个字符。
The Pointer points at the first character of the character chain.
cout << *aString; // H
但是为什么整个字符串保存在指针中?
but why is the whole string saved in the pointer?
cout << aString //Hello Stackoverflow
我希望一个地址,不是地址保存在指针? Hello Stackoverflow的地址在哪里?
I would expect an adress, aren´t adresses saved in pointers? Where is the adress of "Hello Stackoverflow"?
任何帮助非常感谢
推荐答案
有一个运算符<<(ostream& char const *)
的重载,它输出从该指针开始的以null结束的字符串, ostream :: operator
There is an overload for operator<<(ostream&, char const*)
which output the null-terminated string starting at that pointer and which is preferred to the operator ostream::operator<<(void*)
which would have output the address.
想要的地址,转换指针 void *
。
If you want the address, cast the pointer to void*
.
这篇关于char * - 为什么指针中没有地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!