soy novato en C++ y estoy haciendo el curso de c++ emitido por Udacity. En la sesion de pointers estoy aprendiendo a acceder a las direcciones fisica donde se almacenan las variables pero me encuentro con la peculiaridad que la intentar imprimir la direccion fisica de una variable tipo char, esta no se imprime. Que puede estas sucediendo?. Creo que es necesario decir que estoy utilizando el editor ofrecido por la plataforma (https://classroom.udacity.com/courses/ud999/lessons/7761613e-1edd-4ccc-bb67-6faeeac7d97e/concepts/ed81d654-601d-4ea5-9540-b63acefb272e)
Mi codigo es el siguiente:
/*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 es el siguiente:
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
Como ven, no se imprime la direccion para la variable tipo char. Sin embargo, si añado (void *)
antes del pointer de la variable char, funciona!! Que hace exactamente este segmento? (en la solucion del ejercicio estaba esta parte pero explican que hace).
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