-1

I am trying to grow the following array of character arrays:

char  input_channel_name[ 20 ][ 36 ];

The number of columns will always be 36, but the number of rows can vary. When I open my file, I can grab the number of channel names as an integer number_of_channels_in

I have tried to convert it to the follow code (with help of Resizing 2D Arrays in C) without luck:

#define  ROWS 20
#define  COLS 36

main( argc, argv )
    char **input_channel_name;

    input_channel_name = malloc(sizeof *input_channel_name * ROWS);
        if ( input_channel_name )
        {
            for ( size_t i = 0; i < ROWS; i++ )
                input_channel_name[i] = malloc( sizeof *input_channel_name[i] * COLS);
        }

get_input_channels:
    input_file = fopen( input_name, "rb" )
    fread( plotFileHeader.headerFloats, sizeof( float ), 2, input_file );
    number_of_channels_in = plotFileHeader.headerFloats[ N_CHAN_SUB ];
    add_rows = number_of_channels_in - ROWS;

    char **tmp = realloc( input_channel_name, sizeof *input_channel_name * ROWS + add_rows);
    if ( tmp )
    {
        input_channel_name = tmp;
        for ( size_t i = 0; i < add_rows; i++ )
        {
            input_channel_name[ROWS + i] = malloc( sizeof *input_channel_name[ROWS + i] * COLS);
        }
    }

I keep getting an

error C2143: syntax error : missing ';' before 'type'

when I try to set input_channel_name = malloc

Any ideas?

Community
  • 1
  • 1
Das.Rot
  • 638
  • 4
  • 11
  • 25
  • 2
    Please take the time to make your program a proper [mcve]. Prototype main as required, add missing include directives, etc. – StoryTeller - Unslander Monica Feb 14 '17 at 17:50
  • 1
    `main( argc, argv )` --> `int main( int argc, char **argv ){ ... }` – BLUEPIXY Feb 14 '17 at 18:02
  • That's not an "array of `char` arrays", but an pointer to pointer. Completely different datatypes. – too honest for this site Feb 14 '17 at 18:06
  • I apologize, C is my kryptonite and I am working with a project that is over 10,000 lines of code. I am just trying to update the arrays such that I can allocate memory during run-time. I thought it would be a quick fix, but it turns out that is not the case. I will work on slimming my code down, but I don't know how to build a complete and verifiable example when it doesn't compile in the first place. – Das.Rot Feb 14 '17 at 21:51

1 Answers1

0

Your programs downfall is that it treats arrays as pointers. They are in fact not one and the same, and you can't us a char** to a access a true mult-dimensional array directly.

The way to declare a pointer to an array of 36 characters is as follows:

char (*p_arr)[36] = NULL;

To have it point to the first such array in a malloc-ed block you'd proceed normally:

char (*p_arr)[36] = malloc(row_count * sizeof *p_arr);

Or, if you're the typedefing sort:

typedef char c_arr_36[36];
c_arr_36 *p_arr = malloc(row_count * sizeof(c_arr_36));
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • I will try to digest this and make sense of it. Out of curiousity, is the answer at http://stackoverflow.com/questions/39208929/resizing-2d-arrays-in-c incorrect? It appeared to be the most straightforward answer I could find and pretty much used it line for line. Also, in your example, what exactly is the variable for the array? Isn't *p_arr a pointer? Isn't that what I was trying to avoid? – Das.Rot Feb 14 '17 at 21:58
  • @Das.Rot - The answer is not incorrect, but it doesn't deal with true multi-dimensional arrays, which is what you were trying to do. `p_arr` is indeed a pointer, but it's a *pointer-to-an-array*. This is something basic C tutorials seem reluctant to cover. If you wish to learn more, see this post http://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays – StoryTeller - Unslander Monica Feb 14 '17 at 23:00