1

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!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Bassa
  • 53
  • 8

4 Answers4

5

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';
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • So, this is just unique to pointers of type ```char```, in that the name of ```char``` pointer can be used to print out the value pointed to by the ```char``` pointer without having to used ```*```? I am trying to understand someone else's code. – Bassa May 22 '20 at 23:06
  • 1
    Yes, because char behaves differently as compared to other datatypes like int. and c++ has already overloaded methods for char datatype so that you don't have to parse it – Hashir May 22 '20 at 23:10
  • 1
    @Bassa If you have for example a declaration like const char *s = "Hello"; then using this pointer like std::cout << s << '\n'; will output the pointed string. If you will write std::cout << *s << '\n'; then the first character of the string literal will be outputted. – Vlad from Moscow May 22 '20 at 23:11
1

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

Hashir
  • 390
  • 5
  • 20
1

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
Eljay
  • 4,648
  • 3
  • 16
  • 27
0

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.

Mohammad Moridi
  • 367
  • 3
  • 10