Question:
I am newbie to C ++ and I am doing the c ++ course issued by Udacity. In the pointers session I am learning to access the physical addresses where the variables are stored but I find the peculiarity that when trying to print the physical address of a variable type char, it does not print. What could be happening? I think it is necessary to say that I am using the editor offered by the platform ( https://classroom.udacity.com/courses/ud999/lessons/7761613e-1edd-4ccc-bb67-6faeeac7d97e/concepts/ed81d654-601d-4ea5-9540 -b63acefb272e )
My code is the following:
/*For this program print for each variable
**print the value of the variable,
**then print the address where it is stored.
*/
#include<iostream>
#include<string>
int main()
{
int givenInt;
float givenFloat;
double givenDouble ;
std::string givenString;
char givenChar;
std::cin>>givenInt;
std::cin>>givenDouble;
std::cin>>givenFloat;
std::cin>>givenChar;
std::getline(std::cin,givenString);
std::cout<<"The value for givenInt is: "<<givenInt<<" and its address is "<<&givenInt<<"\n";
std::cout<<"The value for givenDouble is: "<<givenDouble<<" and its address is "<<&givenDouble<<"\n";
std::cout<<"The value for givenFloat is: "<<givenFloat<<" and its address is "<<&givenFloat<<"\n";
std::cout<<"The value for givenChar is: "<<givenChar<<" and its address is "<<&givenChar<<"\n";
std::cout<<"The value for givenString is: "<<givenString<<" and its address is "<<&givenString<<"\n";
return 0;
}
My output is the following:
The value for givenInt is: 32 and its address is 0x7fffefdd8418
The value for givenDouble is: 64.212 and its address is 0x7fffefdd8428
The value for givenFloat is: 4.76545 and its address is 0x7fffefdd841c
The value for givenChar is: a and its address is a
The value for givenString is: and its address is 0x7fffefdd8420
As you can see, the address for the variable type char is not printed. However, if I add (void *)
before the char variable pointer, it works !! What exactly does this segment do? (In the solution of the exercise there was this part but they explain what it does).
std::cout<<"The value for givenChar is: "<<givenChar<<" and its address is "<<(void *)&givenChar<<"\n";
el output es:
The value for givenChar is: a and its address is 0x7ffe4da97887
Answer:
Your code is equivalent to this:
char givenChar = 'a';
char* ptr = &givenChar;
std::cout << ptr;
If we take a look at the insertion operator overloads for the cout
object, which is of type ostream
, we find the following:
ostream& operator<< (ostream& os, const char* s);
This overload is used to print character strings:
std::cout << "Hola a todo el mundo";
And the compiler has no way of knowing that what you want here is to remove the memory address of the pointer.
But why does it work correctly in the other cases? Basically because there is no specific overload for a pointer of type int
, neither float
, nor double
, … in these cases the program executes the following overload :
ostream& operator<< (void* val);
Since a pointer of type void
is considered a generic pointer.
For your program to work as you expect, all you have to do is force your pointer to char
so that it passes as a pointer to void
:
std::cout << "The value for givenChar is: "
<< givenChar
<< " and its address is "
<< static_cast<void*>(&givenChar) // <<--- Conversión a void*
<<"\n";