c++ - A function to display contents of 1 or 2 dimensional array of any type -


i needed able display contents of various arrays (for debugging purposes @ point), , decided write function me that. came with. goal able display type of incoming array (int, double, etc). because never had official programming training, wondering if have "inelegant" , improved doing obvious computer science person, not layperson.

int displayarrayindebugwindow( void**              incoming_array, char*               array_type_str, int                 array_last_index_dim_size, int                 array_terminator, hwnd                handle_to_display_window, wchar_t*            optional_array_name     )  {  wchar_t             message_bufferw[1000];     message_bufferw[0] =                                                        l'\0'; wchar_t             temp_buffer[400]; if ( array_last_index_dim_size == 0 ) { array_last_index_dim_size =             1; }  // ---------------------------------------------------------------------------- // processing "int" type array // ---------------------------------------------------------------------------- if (    0 == (strcmp( array_type_str, "int" ))  ) {      int             j =                                                         0;     swprintf( temp_buffer,                                                      l"%s\r\n", optional_array_name );     wcscat( message_bufferw,                                                    temp_buffer );     ( int = 0; ((int)(*((int*)( (int)incoming_array + * (int)sizeof(int) * array_last_index_dim_size + j * (int)sizeof(int))))) != array_terminator; i++ )     {         swprintf( temp_buffer, l"%02i:\t", );         wcscat( message_bufferw,                                                temp_buffer );         ( j; j < last_array_dim_size; j++ )         {             swprintf( temp_buffer, l"%i\t", ((int)(*((int*)( (int)incoming_array +  * (int)sizeof(int) * array_last_index_dim_size + j * (int)sizeof(int) )))) ); //             wcscat( message_bufferw,                                            temp_buffer );         }            wcscat( message_bufferw,                                                l"\r\n" );          // --------------------------------------------------------------------         // reset j 0 each time         // --------------------------------------------------------------------         j =                                                                     0;     }     swprintf( temp_buffer,                                                      l"\nend of array\n" );     wcscat( message_bufferw,                                                    temp_buffer );     setwindowtext( handle_to_display_window, message_bufferw ); }   return 0; } 

nb: when pass in "incoming array", type cast (void**) obviously.

when data type changes algorithm doesn't, it's time consider using templates.

template<class element_type> print_array(element_type const * p_begin,             element_type const * p_end) {   while (p_begin != p_end)   {     cout << *p_begin;     ++p_begin;   } } 

the conversion single dimension multiple dimension left exercise op , readers.

edit 1: alternative
@ point, output function need information how print information gave it.

one option write own printf function has format specifiers data send it.

while option pass pointer function prints data.

the fundamental issue output function needs know how print data.

for c++, suggest overriding operator<< in class / structure. since class/structure knows data, can know how print data.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

how to prompt save As Box in Excel Interlop c# MVC 4 -

xslt 1.0 - How to access or retrieve mets content of an item from another item? -