打印字符数组的地址

4
int *i = new int(1);
cout << i << endl; 

将打印整数的地址。
    char *c="cstring";
    cout << c << endl;
    cout << &(*c) << endl;

两者都会打印“cstring”。我猜这种行为可以简单地解释为在IOstream库中实现了ostream& operator<< (ostream& out, const char* s );

但是如果您实际上想要打印c所指向的数据的地址,该怎么办呢?

3个回答

8
cout << reinterpret_cast<void*>(c) << endl;

或者只是
cout << (void*)c << endl;

1
尝试将其转换为 const void*
cout << static_cast<const void*>(c) << endl;

0

只是怎么样呢

cout << &c << endl;

对我来说可以运行 :)


3
不,它并不会。这将给出指针的地址,而不是该指针所指向的地址。 - Boris Month

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接