C++ 关于指针和什么是/不是指针的问题
关于最终指向指针的问题(我怀疑)。请阅读注释代码中提出的问题:C++ 关于指针和什么是/不是指针的问题,c++,pointers,C++,Pointers,关于最终指向指针的问题(我怀疑)。请阅读注释代码中提出的问题: void doodah(char* a); int main() { char d[] = "message"; // one way of assigning a string. // char* d = "message"; // another way of assigning a string, but REM'ed out for now. cout << d << en
void doodah(char* a);
int main() {
char d[] = "message"; // one way of assigning a string.
// char* d = "message"; // another way of assigning a string, but REM'ed out for now.
cout << d << endl; // d appears not to be a pointer because cout outputs "message", and not an address. Why is this?
doodah(d); // a function call.
}
void doodah(char* a) {
cout << a << endl; // this outputs "message" - but why?! What does 'a' mean in this context?
// cout << *a << endl; // this outputs "m" - but why?! REM'ed out for now.
}
void doodah(char*a);
int main(){
char d[]=“message”;//分配字符串的一种方法。
//char*d=“message”;//另一种分配字符串的方法,但暂时不使用。
coutcout
知道在给定char*
时如何输出字符串。它不会尝试打印指针值本身。数组是内存中某个位置相邻的一组对象。设置数组的变量实际上是指向该数组中第一项的指针(shhh!)
char*c
和char c[]之间的最大区别
是后者将是一个const
指针,而前者可以自由更改。此外,C字符串(如您在此处设置的字符串)以null结尾,这意味着数组以二进制0结尾,因此cout
等内容将知道何时停止迭代(这也称为最后一次一次)
有关更多信息,请阅读。这是内存中指针a
的外观:
<代码> > <代码>是指向字符串的第一个元素的指针。当使用流插入器时(<代码>操作符>代码> cOUT/CODE >将指针打印为<代码> char <代码>,而不是其内存地址。<代码>空隙main < /C>是非法的C++。也许它会帮助您尝试。“std::cout是这里使用的重载?是的,如果我想显示变量的地址?我应该使用&?您必须强制转换到其他内容,例如cout。”
-------------------------------------------------------------
| | 'm' | 'e' | 's' | 's' | 'a' | 'g' | 'e' | '\0' |
| ^ |
| | |
| --- |
| |a| |
| --- |
-------------------------------------------------------------
std::cout << std::addressof(a);