Why does cout print all the string using the pointer?

closed account (jvqpDjzh)
/*
main.cpp
*/
/***********************************************************************/

#include <iostream>
#include <cstring>
using namespace std;
/***********************************************************************/

void printA(const char *str, int len)
{
cout<<str;//why does it print all the string?

//cout<<*str;//prints just the first letter, because str is pointing to the first letter initially

/*prints all the string
for (int i=0; i<len; i++)
{
cout<<*str;
str++;
}
*/
}

/***********************************************************************/

int main(int argc, char** argv) {

const char a [] = "Hello World!";
int len = strlen(a);

cout<<"The string is: ";
printA(a, len);

return 0;
}
Last edited on
cout << str; //why does it print all the string?

For the same reason the following prints out The string is: .

cout << "The string is: ";
closed account (jvqpDjzh)
Are you kidding me?
Are you kidding me?


No, I'm not. Both lines of code use the same overload of operator<<. It works that way because that is the way it was designed to work.
And here is the overloaded operator<< in question:
http://www.cplusplus.com/reference/ostream/ostream/operator-free/

Notice how it's overloaded for single char as well.
If it wasn't, we'd probably see ASCII codes instead of characters.
Last edited on
closed account (jvqpDjzh)
Actually my question wasn't about cout, but how could cout print the value of the variable pointed to by str?!

Shouldn't pointer store the address of the variable it points?

And to access to the variable it points we should first dereference it, right?

In this case cout doesn't need dereference to access to the value of the variable pointed to by str, why?
Last edited on
zwiluwu wrote:
cout doesn't need dereference to access to the value of the variable pointed to by str, why?

As cire has already said:

cire wrote:
It works that way because that is the way it was designed to work.

Since traditional C-style strings are char arrays, it was decided that it would be useful to define the << operator for char* such that it outputs the string pointed to, rather than outputting the address.
Last edited on
closed account (jvqpDjzh)
Thanks to everybody, especially to MikeyBoy! :-)
if you want to print the adress use cout<<(&str);
@SorinAlex: that would be the address of the pointer, which isn't very useful. To print the address of the character that the pointer points to, just cast the pointer to a void pointer.
OK well aparently printf has a special %p type for pointers :


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
#include <cstdio>
int main()
{
    char* str="Hello World";
    cout<<"&str --> "<< &str<<"  (adrres of the pointer)"<<endl;
    cout<<"(void*)str --> "<<(void*)str<<"  (adrres of the data the pointer is pointing to)"<<endl;
    printf("printf *str --> %p",str);
    return 0;
}
Yes, though mixing C++ and C I/O is generally considered bad practice, though harmless 99% of the time.
Topic archived. No new replies allowed.