简体   繁体   中英

Why does the following piece of code return the value pointed at by the pointer and not the address of the pointer?

I have the following two lines of code:

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

I know that the above two lines of code work, but I am not sure why. If we want to print out the value that the pointer is pointing to, should we not use *skimfile ? How is it that the above code uses only skimfile to access the value pointed at by the pointer skimfile ? Does having const in front of the declaration of the pointer make this situation different?

Thank you very much for your help! I really appreciate it!

If you want to output the value of the pointer then write

cout << "skimfile = " << static_cast<void *>( skimfile ) << endl;

Otherwise the operator << for character pointers is overloaded such a way that it outputs a pointed string.

Without such an overloaded operator you had to write for example

const char *s = "Hello";

for ( const char *p = s; *p; ++p )
{
    std::cout << *p;
}
std::cout << '\n';

instead of just

std::cout << s << '\n';

No, it is not due to const . This is exactly how it should work and here is why:

skimfile is a pointer (in this case it is a pointer to const char) - so *skimfile is the object being pointed to by the pointer

so cout << *skimfile should only print the first character.

When you do - cout << skimfile - you are passing the pointer itself. And cin will print out the whole string. In other words it would do cout << *skimfile, then cout << *(skimfile+1), until it has stepped through the whole string.

If you want to print the address you have to cast it to some other type of pointer - casting as a pointer to void is the method most people use. cout << "skimfile = " << (void*)skimfile << endl;

Here is a more detailed answer:

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

The std::ostream such as std::cout is just a binary function operator<< .

For most pointers, the fallback just prints its address.

But char const* is printed expecting a C-style string or C-style string literal. ostream& operator<<(ostream&, char const*); prints the characters until a '\0' stops the loop.

You can simulate that behavior with some simple structs:

#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";
}

Which has the output:

Coord: 10, 20
PCoord: 10, 20
Stuff: 30, 40
PStuff: 0x7ffeeaa06a88

All the point here is that you are going to print the char* variable.

As you may know, in your code skimfile is pointed to the first character of a string of characters. So when you are going to print it, it will continue reading from memory until it gets NULL value, therefore, it will print all the string characters instead of its address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM