1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
enum type_t { INT, DOUBLE, CHAR_ARRAY_10 } ;
void print( const void *arr, int num, type_t type )
{
switch( type )
{
case INT :
{
const int* p = static_cast< const int* >(arr) ;
for( int i=0 ; i<num ; ++i ) std::cout << p[i] << ' ' ;
}
break ;
case DOUBLE :
{
const double* p = static_cast< const double* >(arr) ;
for( int i=0 ; i<num ; ++i ) std::cout << p[i] << ' ' ;
}
break ;
case CHAR_ARRAY_10 :
{
typedef char cstr[10] ;
const cstr* p = static_cast< const cstr* >(arr) ;
for( int i=0 ; i<num ; ++i ) std::cout << p[i] << ' ' ;
}
}
std::cout << '\n' ;
}
int main()
{
enum { N = 3, NCHARS = 10 } ;
const char a[N][NCHARS] = { "abcd", "efgh", "ijkl" } ;
print( a, N, CHAR_ARRAY_10 ) ;
typedef char cstr_t[NCHARS] ;
cstr_t* b = new cstr_t[N] ;
for( int i=0 ; i < N ; ++i )
{
std::string word ;
std::cout << "? " && std::cin >> word ;
std::strcpy( b[i], word.substr( 0, NCHARS-1 ).c_str() ) ;
}
print( b, N, CHAR_ARRAY_10 ) ;
const int c[] = { 0, 1, 2, 3, 4, 5 } ;
print( c, sizeof(c)/sizeof(*c), INT ) ;
const double d[] = { 1.2, 3.4, 5.6, 7.8 } ;
print( d, sizeof(d)/sizeof(*d), DOUBLE ) ;
}
|