为什么下面的代码段返回指针所指向的值而不是指针的地址?

时间:2020-05-22 22:58:42

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

我有以下两行代码:

<a class="menu-accordion">
  @@include('img/icons/arrow-hcp.svg')
  text
</a>
<div class="panel">
  <ul>
    <li><a href="/"> @@include('img/icons/chevron-right-wht.svg')</a></li>
  </ul>
</div>
<a class="menu-accordion">
  @@include('img/icons/arrow-hcp.svg')
</a>
<div class="panel">
  <ul>
    <li><a href="/"> @@include('img/icons/chevron-right-wht.svg')</a></li>
  </ul>
</div>




  var acc = document.getElementsByClassName("menu-accordion");
  var svgarrow = document.querySelector(".menu-accordion > svg");
  var i;
  for (i = 0; i < acc.length; i++) {
    acc[i].addEventListener("click", function () {
      this.classList.toggle("active");
      var panel = this.nextElementSibling;
      if (panel.style.display === "block") { 
        panel.style.display = "none";
        svgarrow.style.transform = "rotate(90deg)";
      } 
      else {
        panel.style.display = "block";
        svgarrow.style.transform = "rotate(-90deg)";
      }
    });
  }

我知道上面的两行代码可以工作,但是我不确定为什么。如果我们要打印出指针所指向的值,是否不应该使用const char* skimfile = argv[3]; cout << "skimfile = " << skimfile << endl; ?上面的代码如何仅使用*skimfile来访问指针skimfile所指向的值?指针声明前面有skimfile会使这种情况有所不同吗?

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

4 个答案:

答案 0 :(得分:5)

如果要输出指针的值,请写

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

答案 1 :(得分:1)

否,不是因为const。这正是它应该如何工作的,这就是原因:

skimfile是一个指针(在这种情况下,它是指向const char的指针)-因此* skimfile是所 指针指向的

所以cout << * * skimfile应该只打印第一个字符。

执行此操作时-cout << Skimfile-您正在传递指针本身。 cin将打印出整个字符串。换句话说,它将执行cout << * skimfile,然后执行cout << **(skimfile + 1),直到逐步遍历整个字符串为止。

如果要打印地址,则必须将其强制转换为其他类型的指针-强制转换 作为指向无效的指针是大多数人使用的方法。 cout <<“ skimfile =” <<(void *)skimfile << endl;

这是更详细的答案:

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

答案 2 :(得分:1)

诸如std :: cout之类的std :: ostream只是二进制函数operator<<

对于大多数指针,后备仅显示其地址。

但是在打印char const*时期望使用C样式字符串或C样式字符串文字。 ostream& operator<<(ostream&, 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

答案 3 :(得分:0)

这里的全部要点是您将打印char*变量。

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