C++ 指针*char和&;烧焦

C++ 指针*char和&;烧焦,c++,pointers,C++,Pointers,我对下面的计划有疑问 #include "stdafx.h" #include "stdio.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { char *chr = "hello"; cout<<chr; //hello cout<<endl; cout<<*chr; //h co

我对下面的计划有疑问

#include "stdafx.h"
#include "stdio.h"
#include <iostream>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

    char *chr = "hello";
    cout<<chr; //hello
     cout<<endl;

     cout<<*chr; //h
     cout<<endl;

    cout<<*(&chr); //hello
system("pause");

    return 0;
}
#包括“stdafx.h”
#包括“stdio.h”
#包括
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
char*chr=“你好”;

cout因为在第二种情况下,您将向cout传递一个
char
,由
chr
指向的char,这是C字符串
的第一个。在第二个示例中,您将取消引用指针。这与使用
chr[0]
相同,在本例中是
h

在上一个示例中,您使用
chr
的地址,然后取消引用:

&chr-->字符**

*(&chr)-->char*


因此,此时,您又有了一个
char*
来更明确一点。
chr
是一个指针,指向存储字符“h”、“e”、“l”、“l”和“o”的内存块。在
chr
的最后一个字符之后是一个终止的
\0
。需要这个指针来表示
chr>所指向的内存块
现在结束

chr
传递给cout时,cout将读取(不是cout本身,而是通过使用
语句调用的底层函数)

cout<<*chr; is same as cout<<chr[0]; 

cout在第三种情况下,您取消引用指向指针的指针,因此将指针传递给cout。语句(&chr)为您提供指向变量chr的临时指针。因此*(&chr)取消引用指针(&chr)。但是chr是指针,因此您只剩下一个指针。
char*chr=“hello”
至少应该给出一个警告。我敢问你正在使用的编译器有多旧吗?@Potatoswatter
\u tmain
提示MSVC。内容如下:“这在C代码中是允许的,但在C++98中被弃用,在C++11中被删除”。我甚至想补充一点,'*'运算符是'&'运算符的逆运算符,因此*(&(*(&(*(&(*(&)(&pointer)'))仍然与指针相同。
cout << chr << endl;
cout << *(&(*(&(*(&(*(&(*(&(*(&(*(&chr))))))))))))) << endl; // I hope i didn't miss a ')' 
cout<<*chr; is same as cout<<chr[0];