简体   繁体   中英

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

I needed to be able to display the contents of my various arrays (for debugging purposes at this point), and decided to write a function to help me with that. This is what I came up with. The goal is to be able to display any type of incoming array (int, double, etc). Because I never had any official programming training, I am wondering if what I have is too "inelegant" and could be improved by doing something obvious to a good computer science person, but not so to a 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 for "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 );
    for ( int i = 0; ((int)(*((int*)( (int)incoming_array + i * (int)sizeof(int) * array_last_index_dim_size + j * (int)sizeof(int))))) != array_terminator; i++ )
    {
        swprintf( temp_buffer, L"%02i:\t", i );
        wcscat( message_bufferw,                                                temp_buffer );
        for ( j; j < last_array_dim_size; j++ )
        {
            swprintf( temp_buffer, L"%i\t", ((int)(*((int*)( (int)incoming_array + i  * (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 to 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 I pass in "incoming array", I type cast it as (void**) obviously.

When the data type changes but the algorithm doesn't, it's time to 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 from single dimension to multiple dimension is left as an exercise to the OP and readers.

Edit 1: Another alternative
At some point, the output function will need information about how to print the information you gave it.

One option is for you to write your own printf function that has format specifiers for the data you send it.

While another option is to pass a pointer to a function that prints the data.

The fundamental issue is that the output function needs to know how to print the data.

For C++, I suggest overriding operator<< in the class / structure. Since the class/structure knows the data, it can easily know how to print the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM