一点关于C++指针用法的小tip,C++ cout后接地址和指针间接引用的值的区别

C++ cout后接地址和指针间接引用的值的区别:
cout接地址,以

#include <iostream>
using namespace std;
void main()
{
"cout后面接地址:" << endl;
 char str[] = "C Language", *p = str;
 cout << p << endl;
 cout << p + 2 << endl;
 cout << &str[7] << endl;//这里cout后面跟地是地址所以用&
 while (*p != '\0') 
  cout << p++ << endl;
 cout << "cout后面接指针间接引用的值:" << endl;
 p = str;//这里必须得重新让指针指向str
 while (*p != '\0')
  cout << *p++;
}

为例:当cout后接地址,则输出的是该地址起到字符串数组结尾的值,当cout后接指针间接引用的值,输出的只是该指针所指地址的值(*p的值)在这里插入图片描述

发布了10 篇原创文章 · 获赞 5 · 访问量 417

猜你喜欢

转载自blog.csdn.net/weixin_40162095/article/details/104257083