How to simulate printf's %p format when using std::cout?
Cast to void*
:
unsigned char* teta = ....;
std::cout << "data at " << static_cast<void*>(teta) << "\n";
iostreams generally assume you have a string with any char*
pointer, but a void*
pointer is just that - an address (simplified), so the iostreams can't do anything other than transforming that address into a string, and not the content of that address.
Where is `%p` useful with printf?
They do not do the same thing. The latter printf
statement interprets b
as an unsigned int
, which is wrong, as b
is a pointer.
Pointers and unsigned int
s are not always the same size, so these are not interchangeable. When they aren't the same size (an increasingly common case, as 64-bit CPUs and operating systems become more common), %x
will only print half of the address. On a Mac (and probably some other systems), that will ruin the address; the output will be wrong.
Always use %p
for pointers.
Why are the outputs of printf and std::cout different?
printf("%d\n", &Foo::c)
: this is undefined behavior, as &Foo::c
is not an integer, but a pointer to member (but, actually, it is usual that the compiler stores pointer to data member as offset, and as 8
is the offset of Foo::c
, 8
is printed).
std::cout << &Foo::c
: this prints the value &Foo::c
. As iostream doesn't have a pointer to member printer, it chooses the closest one: it converts it to bool
, and prints it as integer. As &Foo::c
converted to bool
is true
, 1
is printed.
format cout for pointer
The compiler happens to put the stack allocated variable at a different location in the different versions of the program.
Try including both printf and cout versions in the same program so that they work with the exact same pointer. Then you will see that the two versions behave the same way.
int n = 44;
printf("n = %d \t &n = %x\n", n, &n);
cout<<"n ="<<n<< "\t" <<"&n ="<<hex<<int(&n)<<endl;
As Mr Lister correctly points out, you should use the %p format string when printing pointers in printf.
How to print function pointers with cout?
There actually is an overload of the << operator that looks something like:
ostream & operator <<( ostream &, const void * );
which does what you expect - outputs in hex. There can be no such standard library overload for function pointers, because there are infinite number of types of them. So the pointer gets converted to another type, which in this case seems to be a bool - I can't offhand remember the rules for this.
Edit: The C++ Standard specifies:
4.12 Boolean conversions
1 An rvalue of arithmetic,
enumeration, pointer, or pointer to
member type can be converted to an
rvalue of type bool.
This is the only conversion specified for function pointers.
std::string formatting like sprintf
You can't do it directly, because you don't have write access to the underlying buffer (until C++11; see Dietrich Epp's comment). You'll have to do it first in a c-string, then copy it into a std::string:
char buff[100];
snprintf(buff, sizeof(buff), "%s", "Hello");
std::string buffAsStdStr = buff;
But I'm not sure why you wouldn't just use a string stream? I'm assuming you have specific reasons to not just do this:
std::ostringstream stringStream;
stringStream << "Hello";
std::string copyOfStr = stringStream.str();
How can one print a size_t variable portably using the printf family?
Use the z
modifier:
size_t x = ...;
ssize_t y = ...;
printf("%zu\n", x); // prints as unsigned decimal
printf("%zx\n", x); // prints as hex
printf("%zd\n", y); // prints as signed decimal
Related Topics
Generate a Plane with Triangle Strips
How to Compare Char Variables (C-Strings)
Is It Legal to Write to Std::String
What Is the Purpose of Std::Make_Pair VS the Constructor of Std::Pair
Is It Possible for the Executable to Ask for Administrator Rights? (Windows 7)
What Is This Crazy C++11 Syntax ==> Struct:Bar {} Foo {};
Overriding Return Type in Function Template Specialization
Passing Std::String by Value or Reference
Types of Iterator:Output VS. Input VS. Forward VS. Random Access Iterator
Line by Line C - C++ Code Debugging in Linux Ubuntu
How to Use Gpu::Stream in Opencv
How to Check Deallocation of Memory
Why Use #Define Instead of a Variable
What Happen to Pointers When Vectors Need More Memory and Realocate Memory
C++ Change Function's Variable Argument