Why does streaming a char pointer to cout not print an address?
Overload resolution selects the ostream& operator<<(ostream& o, const char *c);
which is used for printing C-style strings. You want the other ostream& operator<<(ostream& o, const void *p);
to be selected. You are probably best off with a cast here:
cout << static_cast<void *>(cptr) << endl;
Why pointer returns value, not adress?
There is an overload for std::ostream& operator<<
which takes a const char*
and interprets it as a null-terminated string, printing out characters until it finds the null terminator. If you want to print the value of the pointer (the address it holds), you can cast it to void*
.
For example
cout << reinterpret_cast<const void*>(fortunes[2]) << endl;
Printing addresses of vector char 's elements shows garbage
For every primitive data types memory locations are contiguous, except
forchar
. It prints some garbage value on screen.
The "memory locations" are contiguous in the exact same way for both cases. The only difference is in how you're displaying your results. When you do:
cout << "Data: " << vChar[i] << " Address:" << &vChar[i] << "\n";
you're giving std::operator<<(std::basic_ostream)
a char*
, as you're applying &
(address-of) on a single char
1 from the vector
, which makes it treat it as a C-style string -- meaning, it looks for a terminating null. In your case, this null is right after some garbage indeed.2 But you're bound to have some garbage after the vector<int>
just as well, only you're not printing it.3
If you want to get the same printout as you're getting for the vector<int>
, then you could explicitly cast to a void
pointer, so std::cout
will treat it as an address to be printed (overload (7) here), not a string:
cout << "Data: " << vChar[i] << " Address:" << static_cast<void*>(&vChar[i]) << "\n";
In which case the output is:
For char vector Size:4 Capacity:4
Data: a Address:0x1c39810
Data: b Address:0x1c39811
Data: c Address:0x1c39812
Data: d Address:0x1c39813
For int vector Size:4 Capacity:4
Data: 1 Address:0x1c39960
Data: 2 Address:0x1c39964
Data: 3 Address:0x1c39968
Data: 4 Address:0x1c3996c
1 char&
to be precise, as std::vector<T>::operator[]
returns a T&
.
2 Note that looking for this terminating null that wasn't placed there by you constitutes undefined behavior, as it potentially makes you access memory that isn't intended to be accessed for this purpose.
3 You can try and see so for yourself if you perform the reverse casting to make std::cout
treat the vector<int>
elements as C-style strings:
cout << "Data: " << vInt[i] << " Address:" << reinterpret_cast<char*>(&vInt[i]) << "\n";
Again, just remember this means undefined behavior as the printing code will look in memory for the terminating null while you definitely didn't have it there for it to find.
cout with pointers c++
Because an int is (usually) 4 bytes 65
will fit quite neatly into just the first byte and the rest of the memory allocated for the int will be 0
this byte pattern happens to match up very closely to how strings are stored in memory.
So when the memory is accessed through a char*
it will print A
most of the time even though most of the prints were ill formed
int v = 65; // Byte pattern on most machines [65,0,0,0]
int* q = &v;
char** o = (char**)&q; // *o now points to [65,0,0,0] ==> ['A','\0','\0','\0'] ==> "A"
std::cout << o << std::endl;
// input: char**
// printed as: pointer
// result: 012FFCAC
std::cout << *o << std::endl; // Undefined Behaviour
// input: char*
// printed as: null terminated string
// result: "A"
std::cout << **o << std::endl; // Undefined Behaviour
// input: char
// printed as: single character
// result: 'A'
printf("%c",*o); // %c expects a [char] type but is given [pointer to char] ERROR
printf("%p",*o); // %p expects a [pointer] type and is given a [pointer to char] OK
Since a char
is (usually) 1 byte there is no null terminand to stop the printing once it starts and it keeps printing whatever is around in memory until it runs into a 0
or an access violation
char v = 65;
std::cout << &v << std::endl; // &v is not a well-formed null terminated string: Undefined Behaviour
// input: char*
// printed as: null terminated string
// result: "A<whatever other data is around v in memory>"
Related Topics
Is the Memory Allocated for Struct Members Continguous? What If a Struct Member Is an Array
A Warning - Comparison Between Signed and Unsigned Integer Expressions
End of File(Eof) of Standard Input Stream (Stdin)
Is the Order of Iterating Through Std::Map Known (And Guaranteed by the Standard)
Send and Receive a File in Socket Programming in Linux with C/C++ (Gcc/G++)
When to Use Std::Forward to Forward Arguments
Checking for Null Pointer in C/C++
Is It Worth Setting Pointers to Null in a Destructor
Is Std::Cout Guaranteed to Be Initialized
Weird Behavior of Right Shift Operator (1 >> 32)
Inspecting Standard Container (Std::Map) Contents with Gdb
What's the Meaning of Exception Code "Exc_I386_Gpflt"
Cross-Platform Way to Get Line Number of an Ini File Where Given Option Was Found
How to Get the Function Pointer of a Built-In Standard Operator
How to Read the Results of a System() Call in C++
How to Force Linker to Use Shared Library Instead of Static Library