【问题标题】:Why does the following piece of code return the value pointed at by the pointer and not the address of the pointer?为什么下面这段代码返回的是指针指向的值而不是指针的地址?
【发布时间】:2020-09-09 21:22:20
【问题描述】:

我有以下两行代码:

const char* skimfile = argv[3];
cout << "skimfile = " << skimfile << endl;

我知道上面两行代码可以工作,但我不知道为什么。如果我们想打印出指针所指向的值,我们不应该使用*skimfile吗?上面的代码怎么只使用skimfile来访问指针skimfile所指向的值呢?在指针声明前加上const 会使这种情况有所不同吗?

非常感谢您的帮助!我真的很感激!

【问题讨论】:

    标签: c++ pointers operator-overloading constants c-strings


    【解决方案1】:

    这里的重点是您要打印char* 变量。

    您可能知道,在您的代码中,skimfile 指向字符串的第一个字符。所以当你要打印它时,它会继续从内存中读取,直到它得到NULL的值,因此,它会打印所有的字符串字符而不是它的地址。

    【讨论】:

      【解决方案2】:

      如果你想输出指针的值那么写

      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';
      

      【讨论】:

      • 所以,这只是 char 类型的指针所独有的,因为 char 指针的名称可用于打印出 char 指针所指向的值,而不必使用*?我正在尝试理解别人的代码。
      • 是的,因为与其他数据类型(如 int)相比,char 的行为不同。并且 c++ 已经为 char 数据类型重载了方法,因此您不必解析它
      • @Bassa 如果你有一个声明,例如 const char *s = "Hello";然后使用这个指针,如 std::cout
      【解决方案3】:

      std::ostream 如 std::cout 只是一个二进制函数operator&lt;&lt;

      对于大多数指针,回退只打印其地址。

      但是 char const* 被打印出来,期待 C 风格的字符串或 C 风格的字符串文字。 ostream&amp; operator&lt;&lt;(ostream&amp;, char const*); 打印字符,直到 '\0' 停止循环。

      您可以使用一些简单的结构来模拟这种行为:

      #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
      

      【讨论】:

        【解决方案4】:

        不,这不是由于const。这正是它应该如何工作的,原因如下:

        skimfile 是一个指针(在这种情况下它是一个指向 const char 的指针) - 所以 *skimfile 是对象 指针所指向的

        所以 cout

        当你这样做时 - cout

        如果您想打印地址,您必须将其转换为其他类型的指针 - 转换 作为指向 void 的指针是大多数人使用的方法。 cout

        这里有一个更详细的答案:

        https://stackoverflow.com/a/17813845/11979793

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多