Re: casting X* to char*
Tom1s posted:
"A pointer to an object can be explicitly converted to a pointer to an
object of different type.
Here's a little program I threw together for giving the different pointer
sizes on a given platform. On Windows XP, it gives 4 for every one.
#include <iostream>
#include <cstdlib>
#include <cstring>
/* The following are only use for their types */
#include <string>
#include <vector>
#include <typeinfo>
template<unsigned width>
const char* CentreHoriz( const char* const p_in )
{
/* NB:
(1) Uses static data, so be careful with sequence points.
(2) Doesn't check that string isn't too long.
*/
static char buffer[width + 1]; /* Automatic null terminator */
std::memset( buffer, ' ', width * sizeof(*buffer) );
unsigned const len = std::strlen(p_in);
std::memcpy( buffer + width / 2 - len / 2,
p_in,
len);
return buffer;
}
template<class T>
void PrintRow( const char* const p )
{
std::cout
<< '|'
<< CentreHoriz<36>(p)
<< "|| "
<< sizeof(T)
<< " |\n"
<<
"-------------------------------------------------------------\n";
}
int main()
{
std::cout <<
"=============================================================\n"
"| How much memory does a particular pointer type consume? |\n"
"=============================================================\n"
"| Type || Bytes |\n"
"=============================================================\n";
PrintRow<char*>("char*");
PrintRow<short*>("short*");
PrintRow<int*>("int*");
PrintRow<long*>("long*");
PrintRow<float*>("float*");
PrintRow<double*>("double*");
PrintRow<long double*>("long double*");
PrintRow<bool*>("bool*");
PrintRow<wchar_t*>("wchar_t*");
PrintRow<std::string*>("std::string*");
PrintRow<std::vector<std::string>*>("std::vector<std::string>*");
std::cout << '\n';
std::system("PAUSE");
}
-Tom1s