为什么下面这段代码return是指针指向的值而不是指针的地址呢?
Why does the following piece of code return the value pointed at by the pointer and not the address of the pointer?
我有以下两行代码:
const char* skimfile = argv[3];
cout << "skimfile = " << skimfile << endl;
我知道上面两行代码可以工作,但我不确定为什么。如果我们想打印出指针所指向的值,难道不应该使用*skimfile
吗?上面的代码怎么只用skimfile
就访问了指针skimfile
指向的值呢?在指针声明前面加上 const
会使这种情况有所不同吗?
非常感谢您的帮助!非常感谢!
如果要输出指针的值那么写
cout << "skimfile = " << static_cast<void *>( skimfile ) << endl;
否则,字符指针的运算符 << 会重载,从而输出指向的字符串。
如果没有这样的重载运算符,您必须编写例如
const char *s = "Hello";
for ( const char *p = s; *p; ++p )
{
std::cout << *p;
}
std::cout << '\n';
而不仅仅是
std::cout << s << '\n';
不,这不是因为 const
。这正是它应该如何工作的原因:
skimfile 是一个指针(在本例中它是一个指向 const char 的指针)- 所以 *skimfile 是对象
指针指向
所以 cout << *skimfile 应该只打印第一个字符。
当您执行 - cout << skimfile - 您正在传递指针本身。而 cin 将打印出整个字符串。换句话说,它将执行 cout << *skimfile,然后执行 cout << *(skimfile+1),直到它遍历整个字符串。
如果您想打印地址,您必须将其转换为其他类型的指针 - 转换
作为指向 void 的指针是大多数人使用的方法。
cout << "skimfile = " << (void*)skimfile << endl;
这里有更详细的回答:
std::ostream如std::cout只是二元函数operator<<
.
对于大多数指针,回退只打印其地址。
但是 char const*
被打印为期望 C 风格的字符串或 C 风格的字符串文字。 ostream& operator<<(ostream&, char const*);
打印字符直到 '[=15=]'
停止循环。
您可以使用一些简单的结构来模拟该行为:
#include <iostream>
using std::cout;
using std::ostream;
namespace {
struct Coord { int x; int y; };
struct Stuff { int x; int y; };
ostream& operator<<(ostream& out, Coord const& coord) {
out << coord.x << ", " << coord.y;
return out;
}
ostream& operator<<(ostream& out, Coord const* p) {
out << p->x << ", " << p->y;
return out;
}
ostream& operator<<(ostream& out, Stuff const& stuff) {
out << stuff.x << ", " << stuff.y;
return out;
}
} // anon
int main() {
auto coord = Coord{10, 20};
auto stuff = Stuff{30, 40};
auto pcoord = &coord;
auto pstuff = &stuff;
cout << "Coord: " << coord << "\n";
cout << "PCoord: " << pcoord << "\n";
cout << "Stuff: " << stuff << "\n";
cout << "PStuff: " << pstuff << "\n";
}
其中有输出:
Coord: 10, 20
PCoord: 10, 20
Stuff: 30, 40
PStuff: 0x7ffeeaa06a88
这里的重点是您要打印 char*
变量。
您可能知道,在您的代码中,skimfile
指向一串字符的第一个字符。所以当你要打印它时,它会继续从内存中读取直到它得到 NULL
值,因此,它会打印所有字符串字符而不是它的地址。
我有以下两行代码:
const char* skimfile = argv[3];
cout << "skimfile = " << skimfile << endl;
我知道上面两行代码可以工作,但我不确定为什么。如果我们想打印出指针所指向的值,难道不应该使用*skimfile
吗?上面的代码怎么只用skimfile
就访问了指针skimfile
指向的值呢?在指针声明前面加上 const
会使这种情况有所不同吗?
非常感谢您的帮助!非常感谢!
如果要输出指针的值那么写
cout << "skimfile = " << static_cast<void *>( skimfile ) << endl;
否则,字符指针的运算符 << 会重载,从而输出指向的字符串。
如果没有这样的重载运算符,您必须编写例如
const char *s = "Hello";
for ( const char *p = s; *p; ++p )
{
std::cout << *p;
}
std::cout << '\n';
而不仅仅是
std::cout << s << '\n';
不,这不是因为 const
。这正是它应该如何工作的原因:
skimfile 是一个指针(在本例中它是一个指向 const char 的指针)- 所以 *skimfile 是对象 指针指向
所以 cout << *skimfile 应该只打印第一个字符。
当您执行 - cout << skimfile - 您正在传递指针本身。而 cin 将打印出整个字符串。换句话说,它将执行 cout << *skimfile,然后执行 cout << *(skimfile+1),直到它遍历整个字符串。
如果您想打印地址,您必须将其转换为其他类型的指针 - 转换 作为指向 void 的指针是大多数人使用的方法。 cout << "skimfile = " << (void*)skimfile << endl;
这里有更详细的回答:
std::ostream如std::cout只是二元函数operator<<
.
对于大多数指针,回退只打印其地址。
但是 char const*
被打印为期望 C 风格的字符串或 C 风格的字符串文字。 ostream& operator<<(ostream&, char const*);
打印字符直到 '[=15=]'
停止循环。
您可以使用一些简单的结构来模拟该行为:
#include <iostream>
using std::cout;
using std::ostream;
namespace {
struct Coord { int x; int y; };
struct Stuff { int x; int y; };
ostream& operator<<(ostream& out, Coord const& coord) {
out << coord.x << ", " << coord.y;
return out;
}
ostream& operator<<(ostream& out, Coord const* p) {
out << p->x << ", " << p->y;
return out;
}
ostream& operator<<(ostream& out, Stuff const& stuff) {
out << stuff.x << ", " << stuff.y;
return out;
}
} // anon
int main() {
auto coord = Coord{10, 20};
auto stuff = Stuff{30, 40};
auto pcoord = &coord;
auto pstuff = &stuff;
cout << "Coord: " << coord << "\n";
cout << "PCoord: " << pcoord << "\n";
cout << "Stuff: " << stuff << "\n";
cout << "PStuff: " << pstuff << "\n";
}
其中有输出:
Coord: 10, 20
PCoord: 10, 20
Stuff: 30, 40
PStuff: 0x7ffeeaa06a88
这里的重点是您要打印 char*
变量。
您可能知道,在您的代码中,skimfile
指向一串字符的第一个字符。所以当你要打印它时,它会继续从内存中读取直到它得到 NULL
值,因此,它会打印所有字符串字符而不是它的地址。