Zac
Zac

Reputation: 4705

Bidimensional array with one dimension at [1]

I'm working on a maintenance project, and I found this array declaration in a C source file:

char vectorFields [1][FIELD_SIZE];
char vectorValues [1][VALUE_SIZE];

initialized with:

strcpy(vectorFields [0], field); // where char field[128]
strcpy(vectorValues [0], vals);  // where char vals[128]

does this bidimensional declaration with one size of [1] make sense (optimization, readability, ...) or it is just a legacy code that I can safely update to monodimensional array and adjust the initialization code?

Upvotes: 2

Views: 65

Answers (1)

Paul R
Paul R

Reputation: 213059

I can think of a couple of possible reasons as to how this came about:

  1. The row dimension was originally some value > 1 and later was changed to 1. It was easier to leave the declarations like this rather than switch to a simpler declaration and then change every point in the code where these variables are referenced.

  2. There is an API somewhere (perhaps external and/or problematic to change) which accepts 2D arrays where the major dimension is dynamic, e.g.

    void foo(char bar[][FIELD_SIZE], int num_bars);

However if you have access to all the places where these variables are referenced and there are no obvious constraints such as (2) above then I don't see any reason not to rationalise this usage.

Upvotes: 2

Related Questions