This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 4 years ago.
I am learning c++ and encountered this:
#include<iostream>
using namespace std;
int main(){
const char *a[] = {"ge","hy"};
cout<<a<<" "<<&a[1]<<endl;
cout<<a[0]<<" "<<a[1];
return 0;
}
The output is :
0x7fff54e71830 0x7fff54e71838
ge hy
I tried to understand the code.
Here is my understanding:
a is an array of character pointers which means that each element of the array is a char pointer.
Now, since every element is a pointer then it should store the address of "ge" and "hy" respectively.
-----------------------------------
a = | 0x7fff54e71830 | 0x7fff54e71838 |
-----------------------------------
Now when I write a[0] and a[1] then why does it print the ge hy and not the memory address of them because the array a stores their address and not their actual value.
I am sure that I'm going wrong somewhere because the output is not as expected. Kindly, correct me here.
The standard library provides an overloaded operator<<(std::ostream&, const char*) for printing C-style strings that prints the string pointed to rather than the value of the pointer itself. That overload is a better match than the operator<<(std::ostream&, void*) overload that prints the address stored in a pointer.
If you want to print the value of the pointer, add a cast to void*:
std::cout << static_cast<void*>(a[0]) << ' ' << static_cast<void*>(a[1]) << '\n';
Live Demo
Related
This question already has answers here:
Why is address of char data not displayed?
(8 answers)
Closed 2 years ago.
Code Snippet :
#include <iostream>
int main()
{
char c = 50;
int i = 50;
std::cout<<&c<<"\n";
std::cout<<*(&c)<<"\n";
std::cout<<&i<<"\n";
return 0;
}
Expected output :
<memory_address>
2
<memory_address>
Original output :
2
2
0x7ffee0504ac4
Confusion :
I thought & in this case should return the reference of the variable, printing which (according to me) should give me a memory address, which turns out to be true with variables of other data type like int,float and double
then why in the case of type char, it is displaying the value in it ?
compiler = clang-1103.0.32.62
Its because of overload for operator<< (const char*) for ostream.
&c is of type char* and the operator<< tries to print 0-terminated C-string.
If you cast to void* as (void *)(&c) then you will get memory address as expected:
#include <iostream>
int main()
{
char c = 50;
int i = 50;
std::cout<<&c<<"\n"; // this will try to print null-terminated char* string.
std::cout << (void*)(&c) << "\n"; // this will print memory address as expected
std::cout<<*(&c)<<"\n";
std::cout<<&i<<"\n";
return 0;
}
Note: cout << &c may print some more characters, depending what bytes are present behind memory address &c ( the 0-byte could be far behind). So this is basically undefined behavior (UB).
There are no references in your code. & is the address-of operator which returns a pointer not a reference. & only means reference when it's used in a declaration, not when it's used in an expression.
The reason you get different results for &c and &i is that operator<< has an overload for char* which assumes that the argument is a C string. Since that's not the case for you, your program actually has undefined behaviour.
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);
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Printing C++ int pointer vs char pointer
(3 answers)
Closed 4 years ago.
Keep in mind that my knowledge of pointers is quite small, as I just started learning about them.
While I was messing around in C++, I wrote this small bit of code thinking it would just print out the address of each character in the string
#include <iostream>
using namespace std;
string a = "Hello, World!";
int main() {
for(int i=0; i<a.length();i++) {
cout << &a[i] << endl;
}
return 0;
}
When I compiled and ran this, however, it resulted in it printing as if the string moved to the left.
It just doesn't make sense why when it uses &, which I thought would retrieve the address, would instead get the rest of the string.
As said in the comment, &a[i] is a pointer to a char, and << operator will print null terminated string starting from this character, not its address. So if you want to print the address, you must cast it to void *, as follow :
#include <iostream>
using namespace std;
string a = "Hello, World!";
int main() {
for(int i=0; i<a.length();i++) {
cout << (void *)&a[i] << endl; //cast to (void *) to get the address
}
return 0;
}
string subscript ([]) operator returns char. So & operation returns a pointer to char. And cout operator<< has an overloading for it, which consider it should print out the parameter as a c-string. You should cast it to void* so cout wouldn't think it is a string.
(void*)&a[i]
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.
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.