Why does calling cout.operator<<(const char*) print the address instead of the character string?How to...
How to have poached eggs in "sphere form"?
On the sensitivity conjecture?
When encrypting twice with two separate keys, can a single key decrypt both steps?
Is Ear Protection Necessary For General Aviation Airplanes?
Create two random teams from a list of players
Security measures that could plausibly last 150+ years?
Why did I lose on time with 3 pawns vs Knight. Shouldn't it be a draw?
Can you continue the movement of a Bonus Action Dash granted by Expeditious Retreat if your Concentration is broken mid-move?
What is this kind of symbol meant to be?
Is it possible for a particle to decay via gravity?
Exploiting the delay when a festival ticket is scanned
What is the highest achievable score in Catan
How did astronauts using rovers tell direction without compasses on the Moon?
How do you deal with characters with multiple races?
Should I intervene when a colleague in a different department makes students run laps as part of their grade?
How can Paypal know my card is being used in another account?
How to choose using Collection<Id> rather than Collection<String>, or the opposite?
What are the closest international airports in different countries?
Was Donald Trump at ground zero helping out on 9-11?
PCB design using code instead of clicking a mouse?
Unknown indication below upper stave
How can flights operated by the same company have such different prices when marketed by another?
Can I attune a Circlet of Human Perfection to my animated skeletons to allow them to blend in and speak?
Why would anyone ever invest in a cash-only etf?
Why does calling cout.operator
How to convert a std::string to const char* or char*?How can I convert a std::basic_string type to an array of char type?Why does outputting a class with a conversion operator not work for std::string?Getting very long “No match for 'operator+'” error in C++How do you compare a string to a vector value?how to print const char pointer in c++Why does printing a char* give a string rather than an address?How to convert const *char to std::stringAddition of two matrix using structc++ error: cannot convert basic_string<char>}' to 'const char*' for argument '1' to 'long int strtol
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I was exploring the ostream
class in C++. I am stuck on the strange output of cout
on string and integer data types.
When passing an integer or floating-point value, the output is exactly what I pass. For example cout.operator<<(10);
prints 10
. But when passing a string as an argument it is printing some hexadecimal values:
#include <iostream>
#include <string>
using namespace std;
int main() {
const char* str = "aia";
cout.operator<<(str);
return 0;
}
Output: 0x4007e0
.
c++ c++11 c++14
|
show 1 more comment
I was exploring the ostream
class in C++. I am stuck on the strange output of cout
on string and integer data types.
When passing an integer or floating-point value, the output is exactly what I pass. For example cout.operator<<(10);
prints 10
. But when passing a string as an argument it is printing some hexadecimal values:
#include <iostream>
#include <string>
using namespace std;
int main() {
const char* str = "aia";
cout.operator<<(str);
return 0;
}
Output: 0x4007e0
.
c++ c++11 c++14
3
Why are you usingoperator<<
directly like that? What are you trying to do?
– Nicol Bolas
8 hours ago
@NicolBolas I suspect that they don't know what to do.
– john
8 hours ago
1
I am trying to understand how the cascading of operators actually works
– Pranjal Kaler
8 hours ago
1
Assuming you don't have some particular reason for using that form of operator<<, just usecout << str;
and you'll see the output you expect.
– john
8 hours ago
1
i tried and it prints the string but my question is why it is printing some hexa values when it can print same result for other data types
– Pranjal Kaler
8 hours ago
|
show 1 more comment
I was exploring the ostream
class in C++. I am stuck on the strange output of cout
on string and integer data types.
When passing an integer or floating-point value, the output is exactly what I pass. For example cout.operator<<(10);
prints 10
. But when passing a string as an argument it is printing some hexadecimal values:
#include <iostream>
#include <string>
using namespace std;
int main() {
const char* str = "aia";
cout.operator<<(str);
return 0;
}
Output: 0x4007e0
.
c++ c++11 c++14
I was exploring the ostream
class in C++. I am stuck on the strange output of cout
on string and integer data types.
When passing an integer or floating-point value, the output is exactly what I pass. For example cout.operator<<(10);
prints 10
. But when passing a string as an argument it is printing some hexadecimal values:
#include <iostream>
#include <string>
using namespace std;
int main() {
const char* str = "aia";
cout.operator<<(str);
return 0;
}
Output: 0x4007e0
.
c++ c++11 c++14
c++ c++11 c++14
edited 6 hours ago
Boann
38.5k13 gold badges93 silver badges123 bronze badges
38.5k13 gold badges93 silver badges123 bronze badges
asked 8 hours ago
Pranjal KalerPranjal Kaler
476 bronze badges
476 bronze badges
3
Why are you usingoperator<<
directly like that? What are you trying to do?
– Nicol Bolas
8 hours ago
@NicolBolas I suspect that they don't know what to do.
– john
8 hours ago
1
I am trying to understand how the cascading of operators actually works
– Pranjal Kaler
8 hours ago
1
Assuming you don't have some particular reason for using that form of operator<<, just usecout << str;
and you'll see the output you expect.
– john
8 hours ago
1
i tried and it prints the string but my question is why it is printing some hexa values when it can print same result for other data types
– Pranjal Kaler
8 hours ago
|
show 1 more comment
3
Why are you usingoperator<<
directly like that? What are you trying to do?
– Nicol Bolas
8 hours ago
@NicolBolas I suspect that they don't know what to do.
– john
8 hours ago
1
I am trying to understand how the cascading of operators actually works
– Pranjal Kaler
8 hours ago
1
Assuming you don't have some particular reason for using that form of operator<<, just usecout << str;
and you'll see the output you expect.
– john
8 hours ago
1
i tried and it prints the string but my question is why it is printing some hexa values when it can print same result for other data types
– Pranjal Kaler
8 hours ago
3
3
Why are you using
operator<<
directly like that? What are you trying to do?– Nicol Bolas
8 hours ago
Why are you using
operator<<
directly like that? What are you trying to do?– Nicol Bolas
8 hours ago
@NicolBolas I suspect that they don't know what to do.
– john
8 hours ago
@NicolBolas I suspect that they don't know what to do.
– john
8 hours ago
1
1
I am trying to understand how the cascading of operators actually works
– Pranjal Kaler
8 hours ago
I am trying to understand how the cascading of operators actually works
– Pranjal Kaler
8 hours ago
1
1
Assuming you don't have some particular reason for using that form of operator<<, just use
cout << str;
and you'll see the output you expect.– john
8 hours ago
Assuming you don't have some particular reason for using that form of operator<<, just use
cout << str;
and you'll see the output you expect.– john
8 hours ago
1
1
i tried and it prints the string but my question is why it is printing some hexa values when it can print same result for other data types
– Pranjal Kaler
8 hours ago
i tried and it prints the string but my question is why it is printing some hexa values when it can print same result for other data types
– Pranjal Kaler
8 hours ago
|
show 1 more comment
3 Answers
3
active
oldest
votes
When you do cout.operator<<(str)
you call cout
's operator <<
member function. If we look at what member functions overloads cout
has we have
basic_ostream& operator<<( short value );
basic_ostream& operator<<( unsigned short value );
basic_ostream& operator<<( int value );
basic_ostream& operator<<( unsigned int value );
basic_ostream& operator<<( long value );
basic_ostream& operator<<( unsigned long value );
basic_ostream& operator<<( long long value );
basic_ostream& operator<<( unsigned long long value );
basic_ostream& operator<<( float value );
basic_ostream& operator<<( double value );
basic_ostream& operator<<( long double value );
basic_ostream& operator<<( bool value );
basic_ostream& operator<<( const void* value );
basic_ostream& operator<<( std::nullptr_t );
basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb);
basic_ostream& operator<<(
std::ios_base& (*func)(std::ios_base&) );
basic_ostream& operator<<(
std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) );
basic_ostream& operator<<(
std::basic_ostream<CharT,Traits>& (*func)(std::basic_ostream<CharT,Traits>&) );
If you notice, there isn't one for a const char*
, but there is one for a const void*
. So, your const char*
is converted to a const void*
and that version of the function prints the address held by the pointer.
What you need to do is call the non member function overload of operator<<
and to do that you can use
cout << str;
1
Always usecout << X
, never usecout.operator<<(X)
oroperator<<(cout, X)
directly (unless you have a REALLY GOOD reason to do so). Let the compiler decide which one to call based on context and data type.
– Remy Lebeau
8 hours ago
@RemyLebeau I've removed theoperator<<(cout, X)
call. As you said, they should just use the operator syntax.
– NathanOliver
8 hours ago
add a comment |
The problem is that for some types operator<<
is overloaded as a member of ostream
and for some types it is overloaded as a global function. In the case of const char*
it's a global function, so if you want to call the operator function explicitly you must write
operator<<(cout, str);
but for integer types you must write
cout.operator<<(num);
What's happening in the code you posted is that the overload for const void*
is being called, which is why you see hexadecimal numbers.
this makes sense. thank you
– Pranjal Kaler
8 hours ago
add a comment |
Use cout << str instead of cout.operator<<(str);
It will work.
New contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57296899%2fwhy-does-calling-cout-operatorconst-char-print-the-address-instead-of-the-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you do cout.operator<<(str)
you call cout
's operator <<
member function. If we look at what member functions overloads cout
has we have
basic_ostream& operator<<( short value );
basic_ostream& operator<<( unsigned short value );
basic_ostream& operator<<( int value );
basic_ostream& operator<<( unsigned int value );
basic_ostream& operator<<( long value );
basic_ostream& operator<<( unsigned long value );
basic_ostream& operator<<( long long value );
basic_ostream& operator<<( unsigned long long value );
basic_ostream& operator<<( float value );
basic_ostream& operator<<( double value );
basic_ostream& operator<<( long double value );
basic_ostream& operator<<( bool value );
basic_ostream& operator<<( const void* value );
basic_ostream& operator<<( std::nullptr_t );
basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb);
basic_ostream& operator<<(
std::ios_base& (*func)(std::ios_base&) );
basic_ostream& operator<<(
std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) );
basic_ostream& operator<<(
std::basic_ostream<CharT,Traits>& (*func)(std::basic_ostream<CharT,Traits>&) );
If you notice, there isn't one for a const char*
, but there is one for a const void*
. So, your const char*
is converted to a const void*
and that version of the function prints the address held by the pointer.
What you need to do is call the non member function overload of operator<<
and to do that you can use
cout << str;
1
Always usecout << X
, never usecout.operator<<(X)
oroperator<<(cout, X)
directly (unless you have a REALLY GOOD reason to do so). Let the compiler decide which one to call based on context and data type.
– Remy Lebeau
8 hours ago
@RemyLebeau I've removed theoperator<<(cout, X)
call. As you said, they should just use the operator syntax.
– NathanOliver
8 hours ago
add a comment |
When you do cout.operator<<(str)
you call cout
's operator <<
member function. If we look at what member functions overloads cout
has we have
basic_ostream& operator<<( short value );
basic_ostream& operator<<( unsigned short value );
basic_ostream& operator<<( int value );
basic_ostream& operator<<( unsigned int value );
basic_ostream& operator<<( long value );
basic_ostream& operator<<( unsigned long value );
basic_ostream& operator<<( long long value );
basic_ostream& operator<<( unsigned long long value );
basic_ostream& operator<<( float value );
basic_ostream& operator<<( double value );
basic_ostream& operator<<( long double value );
basic_ostream& operator<<( bool value );
basic_ostream& operator<<( const void* value );
basic_ostream& operator<<( std::nullptr_t );
basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb);
basic_ostream& operator<<(
std::ios_base& (*func)(std::ios_base&) );
basic_ostream& operator<<(
std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) );
basic_ostream& operator<<(
std::basic_ostream<CharT,Traits>& (*func)(std::basic_ostream<CharT,Traits>&) );
If you notice, there isn't one for a const char*
, but there is one for a const void*
. So, your const char*
is converted to a const void*
and that version of the function prints the address held by the pointer.
What you need to do is call the non member function overload of operator<<
and to do that you can use
cout << str;
1
Always usecout << X
, never usecout.operator<<(X)
oroperator<<(cout, X)
directly (unless you have a REALLY GOOD reason to do so). Let the compiler decide which one to call based on context and data type.
– Remy Lebeau
8 hours ago
@RemyLebeau I've removed theoperator<<(cout, X)
call. As you said, they should just use the operator syntax.
– NathanOliver
8 hours ago
add a comment |
When you do cout.operator<<(str)
you call cout
's operator <<
member function. If we look at what member functions overloads cout
has we have
basic_ostream& operator<<( short value );
basic_ostream& operator<<( unsigned short value );
basic_ostream& operator<<( int value );
basic_ostream& operator<<( unsigned int value );
basic_ostream& operator<<( long value );
basic_ostream& operator<<( unsigned long value );
basic_ostream& operator<<( long long value );
basic_ostream& operator<<( unsigned long long value );
basic_ostream& operator<<( float value );
basic_ostream& operator<<( double value );
basic_ostream& operator<<( long double value );
basic_ostream& operator<<( bool value );
basic_ostream& operator<<( const void* value );
basic_ostream& operator<<( std::nullptr_t );
basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb);
basic_ostream& operator<<(
std::ios_base& (*func)(std::ios_base&) );
basic_ostream& operator<<(
std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) );
basic_ostream& operator<<(
std::basic_ostream<CharT,Traits>& (*func)(std::basic_ostream<CharT,Traits>&) );
If you notice, there isn't one for a const char*
, but there is one for a const void*
. So, your const char*
is converted to a const void*
and that version of the function prints the address held by the pointer.
What you need to do is call the non member function overload of operator<<
and to do that you can use
cout << str;
When you do cout.operator<<(str)
you call cout
's operator <<
member function. If we look at what member functions overloads cout
has we have
basic_ostream& operator<<( short value );
basic_ostream& operator<<( unsigned short value );
basic_ostream& operator<<( int value );
basic_ostream& operator<<( unsigned int value );
basic_ostream& operator<<( long value );
basic_ostream& operator<<( unsigned long value );
basic_ostream& operator<<( long long value );
basic_ostream& operator<<( unsigned long long value );
basic_ostream& operator<<( float value );
basic_ostream& operator<<( double value );
basic_ostream& operator<<( long double value );
basic_ostream& operator<<( bool value );
basic_ostream& operator<<( const void* value );
basic_ostream& operator<<( std::nullptr_t );
basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb);
basic_ostream& operator<<(
std::ios_base& (*func)(std::ios_base&) );
basic_ostream& operator<<(
std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) );
basic_ostream& operator<<(
std::basic_ostream<CharT,Traits>& (*func)(std::basic_ostream<CharT,Traits>&) );
If you notice, there isn't one for a const char*
, but there is one for a const void*
. So, your const char*
is converted to a const void*
and that version of the function prints the address held by the pointer.
What you need to do is call the non member function overload of operator<<
and to do that you can use
cout << str;
edited 8 hours ago
answered 8 hours ago
NathanOliverNathanOliver
110k19 gold badges167 silver badges246 bronze badges
110k19 gold badges167 silver badges246 bronze badges
1
Always usecout << X
, never usecout.operator<<(X)
oroperator<<(cout, X)
directly (unless you have a REALLY GOOD reason to do so). Let the compiler decide which one to call based on context and data type.
– Remy Lebeau
8 hours ago
@RemyLebeau I've removed theoperator<<(cout, X)
call. As you said, they should just use the operator syntax.
– NathanOliver
8 hours ago
add a comment |
1
Always usecout << X
, never usecout.operator<<(X)
oroperator<<(cout, X)
directly (unless you have a REALLY GOOD reason to do so). Let the compiler decide which one to call based on context and data type.
– Remy Lebeau
8 hours ago
@RemyLebeau I've removed theoperator<<(cout, X)
call. As you said, they should just use the operator syntax.
– NathanOliver
8 hours ago
1
1
Always use
cout << X
, never use cout.operator<<(X)
or operator<<(cout, X)
directly (unless you have a REALLY GOOD reason to do so). Let the compiler decide which one to call based on context and data type.– Remy Lebeau
8 hours ago
Always use
cout << X
, never use cout.operator<<(X)
or operator<<(cout, X)
directly (unless you have a REALLY GOOD reason to do so). Let the compiler decide which one to call based on context and data type.– Remy Lebeau
8 hours ago
@RemyLebeau I've removed the
operator<<(cout, X)
call. As you said, they should just use the operator syntax.– NathanOliver
8 hours ago
@RemyLebeau I've removed the
operator<<(cout, X)
call. As you said, they should just use the operator syntax.– NathanOliver
8 hours ago
add a comment |
The problem is that for some types operator<<
is overloaded as a member of ostream
and for some types it is overloaded as a global function. In the case of const char*
it's a global function, so if you want to call the operator function explicitly you must write
operator<<(cout, str);
but for integer types you must write
cout.operator<<(num);
What's happening in the code you posted is that the overload for const void*
is being called, which is why you see hexadecimal numbers.
this makes sense. thank you
– Pranjal Kaler
8 hours ago
add a comment |
The problem is that for some types operator<<
is overloaded as a member of ostream
and for some types it is overloaded as a global function. In the case of const char*
it's a global function, so if you want to call the operator function explicitly you must write
operator<<(cout, str);
but for integer types you must write
cout.operator<<(num);
What's happening in the code you posted is that the overload for const void*
is being called, which is why you see hexadecimal numbers.
this makes sense. thank you
– Pranjal Kaler
8 hours ago
add a comment |
The problem is that for some types operator<<
is overloaded as a member of ostream
and for some types it is overloaded as a global function. In the case of const char*
it's a global function, so if you want to call the operator function explicitly you must write
operator<<(cout, str);
but for integer types you must write
cout.operator<<(num);
What's happening in the code you posted is that the overload for const void*
is being called, which is why you see hexadecimal numbers.
The problem is that for some types operator<<
is overloaded as a member of ostream
and for some types it is overloaded as a global function. In the case of const char*
it's a global function, so if you want to call the operator function explicitly you must write
operator<<(cout, str);
but for integer types you must write
cout.operator<<(num);
What's happening in the code you posted is that the overload for const void*
is being called, which is why you see hexadecimal numbers.
answered 8 hours ago
johnjohn
43.1k2 gold badges29 silver badges50 bronze badges
43.1k2 gold badges29 silver badges50 bronze badges
this makes sense. thank you
– Pranjal Kaler
8 hours ago
add a comment |
this makes sense. thank you
– Pranjal Kaler
8 hours ago
this makes sense. thank you
– Pranjal Kaler
8 hours ago
this makes sense. thank you
– Pranjal Kaler
8 hours ago
add a comment |
Use cout << str instead of cout.operator<<(str);
It will work.
New contributor
add a comment |
Use cout << str instead of cout.operator<<(str);
It will work.
New contributor
add a comment |
Use cout << str instead of cout.operator<<(str);
It will work.
New contributor
Use cout << str instead of cout.operator<<(str);
It will work.
New contributor
New contributor
answered 7 hours ago
XurahbeelXurahbeel
71 bronze badge
71 bronze badge
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57296899%2fwhy-does-calling-cout-operatorconst-char-print-the-address-instead-of-the-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Why are you using
operator<<
directly like that? What are you trying to do?– Nicol Bolas
8 hours ago
@NicolBolas I suspect that they don't know what to do.
– john
8 hours ago
1
I am trying to understand how the cascading of operators actually works
– Pranjal Kaler
8 hours ago
1
Assuming you don't have some particular reason for using that form of operator<<, just use
cout << str;
and you'll see the output you expect.– john
8 hours ago
1
i tried and it prints the string but my question is why it is printing some hexa values when it can print same result for other data types
– Pranjal Kaler
8 hours ago