Meaning of output [duplicate] - c++

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 5 years ago.
char p;
cout << &p;
This does not print the address of character p. It prints some characters. Why?
char p;
char *q;
q = &p;
cout << q;
Even this does not. Why?

I believe the << operator recognizes it as a string. Casting it to a void* should work:
cout << (void*)&p;
std::basic_ostream has a specialized operator that takes a std::basic_streambuf (which basically is a string (in this case)):
_Myt& operator<<(_Mysb *_Strbuf)
as opposed to the operator that takes any pointer (except char* of course):
_Myt& operator<<(const void *_Val)

This is because the pointer to char has its own overload of <<, which interprets the pointer as a C string.
You can fix your code by adding a cast to void*, which is the overload that prints a pointer:
char p;
cout << (void*)&p << endl;
Demo 1.
Note that the problem happens for char pointer, but not for other kinds of pointers. Say, if you use int instead of char in your declaration, your code would work without a cast:
int p;
cout << &p << endl;
Demo 2.

std::cout will treat a char* as a string. You are basically seeing whatever is contained in memory at the location of your uninitialised pointer - until a terminating null character is encountered. Casting the pointer to a void* should print the actual pointer value if you need to see it

Related

Why c_str() doesn't output an address when it returns a const pointer value [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 3 years ago.
I'm studying pointer and got confused with c_str() function.
The description says it returns a constant Null terminated pointer to the character array and when i try to print the variable, it doesn't print the address of it.
int main() {
string a = "hello";
const char* b = a.c_str();
cout << b << endl; //hello
}
I expected the output to be the address of b but instead it prints "hello".
Can someone explain why?
There's an overload of operator<< for std::ostream and const char*, that will print the string rather than the pointer value (address).
You can cast to const void* to invoke the overload that prints the address.
cout << static_cast<const void*>(b);

Why char array and int array get printed as different things? [duplicate]

This question already has answers here:
Why does cout print char arrays differently from other arrays?
(4 answers)
Closed 7 years ago.
int main ()
{
int intarr [5] = {1,6,7,9,3);
char charr [7] = "Avneet";
std::cout << intarr << "\n";
std::cout << charr << "\n";
return 0;
}
The first line this program prints is a memory address and second line, the whole string (Avneet). What's making the difference? One more question. In the int case, the address I get is what? The address of the entire array or just the address of the first element of that array.
std::basic_ostream<> has overloads of operator<< for char const* and signed/unsigned versions that expect a C-style zero-terminated string.
And void const* overload which is used for any other pointer types and outputs the address.
The std::cout operator << is overloaded for const char* in such a way that the string that it points to is printed out. This is not the case for int* arguments.
When an array is passed (by value) as an argument to a function the argument decays to a pointer to the first element in the array and that is why you get the behaviour you are seeing - the char array argument decays to char* and the cout operator << overload prints the string.
In the int* case there is no overload and so you just see the address of the first element of your int array printed.
I would say that when creating a char[7] it acts like a String of 7 characters. When printing that will not show its address but the value.
On the other hand the int array is an object which holds some primitives. When printing it you will simply see that objects address. You need a for loop to print each of the arrays elements.

char array[] weird output [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 7 years ago.
char a[] = {'k','l','m'};
cout << a << endl;
int b[] = {1,2,3};
cout << b << endl;
I run the above C++ code and here's the output:
klm
0x22fe00
I observe that char is the only primary type that has this behavior. Why this is happening? Are there any specialities of the char type?
The name of an array often evaluates to the address of its first element. The standard output stream interprets character pointers as strings, and prints the data as a string. For integers, there is no such interpretation so you see the actual pointer value.
The char[] is essentially how C and C++ treat strings of characters. The operator<< has been overloaded for the char[] to print out the values of the char array. On the other hand, arrays are essentially treated as constant pointers to their base element:
const int* p = &b[0];
Therefore, when you do cout << b << endl, you're actually printing out the base address of the array. That's why you get the hex number.

"cout" and "char address" [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 5 years ago.
char p;
cout << &p;
This does not print the address of character p. It prints some characters. Why?
char p;
char *q;
q = &p;
cout << q;
Even this does not. Why?
I believe the << operator recognizes it as a string. Casting it to a void* should work:
cout << (void*)&p;
std::basic_ostream has a specialized operator that takes a std::basic_streambuf (which basically is a string (in this case)):
_Myt& operator<<(_Mysb *_Strbuf)
as opposed to the operator that takes any pointer (except char* of course):
_Myt& operator<<(const void *_Val)
This is because the pointer to char has its own overload of <<, which interprets the pointer as a C string.
You can fix your code by adding a cast to void*, which is the overload that prints a pointer:
char p;
cout << (void*)&p << endl;
Demo 1.
Note that the problem happens for char pointer, but not for other kinds of pointers. Say, if you use int instead of char in your declaration, your code would work without a cast:
int p;
cout << &p << endl;
Demo 2.
std::cout will treat a char* as a string. You are basically seeing whatever is contained in memory at the location of your uninitialised pointer - until a terminating null character is encountered. Casting the pointer to a void* should print the actual pointer value if you need to see it

C++ - Null Pointers [duplicate]

This question already has answers here:
Why does std::cout output disappear completely after NULL is sent to it
(3 answers)
Closed 8 years ago.
I'm learning about pointers in C++. I wrote this simple program to show what I had a problem with:
#include <iostream>
using namespace std;
int main() {
cout << "test1";
char *ptr = 0;
cout << ptr;
cout << "test2";
}
When I run the program, it doesn't output "test2" at the end, instead only "test1". This should mean that it crashed when I tried to print out the value of ptr? I tried stepping through it in Eclipse debugger and it looks like every line gets executed but doesn't it throw an error or something?
char *ptr = 0;
cout << ptr;
There's an overload of the << operator that takes a char* operand, which it assumes is a pointer to a C-style string.
For pointer types other than char*, the << operator would print the value of the pointer (which is an address), but treating a null char* pointer as if it pointed to a C-style string causes undefined behavior. In any case, it's not going to print the pointer value.
To print the pointer value, you can convert it to void*:
cout << "test1\n";
char *ptr = 0;
cout << static_cast<void*>(ptr) << "\n";
cout << "test2" << "\n";;
Normally you can output a pointer to cout and it will print the address contained. However, when you output a char * it is interpreted as a C-style null-terminated string. In this case, it's a null pointer and does not point to a string.
Try casting it to a void * before outputting it.