Q:

2d array for header/detail/trailer fields

I am using Linux 6.
I have a data file with fixed record length, i.e. each line is of same number of char and is delimited by LINE-FEED char.
The file has a line of header and trailer with many details lines.
There is no field delimiter in the file.  So, I have to use another file (called format file) to denote the fields.
Then, I will read the format file into a 2D array and then process the data file accordingly.
I use a 2D array to store the fields metadata.  Here is the 2D array structure:
#define HEADER  0
#define DETAIL  1
#define TRAILER 2

struct _field {
       char  fieldName[64] ;
       int   startColumn   ;
       int   endColumn     ;
       char  fieldType     ;
} fields[3][500] ;

int numFields[3] ;   // count the number of fields in header/detail/trailer

The first [3] is for header/detail/trailer.
At most, there are 500 fields in the details-line.  But, for header/trailer line, there are at most 10 fields.
I use this 2D array to decode all the fields in the header/detail/trailer lines of the file.
This 2D array is a quick-and-dirty method for the C program.  For the header/trailer, the array[500] is really a waste.
For easy programming, I do NOT want to use ANOTHER structure with same layout for header/trailer.  I still prefer to use this kind of 2D array.
Is there any method that I do not need to waste so much ... memory in the array ?
What I want is something like fields[0][10] , fields[1][500] and fields[2][10] ?

A:

Change fields into array of pointers.
struct _field {
       char  fieldName[64] ;
       int   startColumn   ;
       int   endColumn     ;
       char  fieldType     ;
} *fields[3];

fields[0] = malloc(10 * sizeof(struct _field));
fields[1] = malloc(500 * sizeof(struct _field));
fields[2] = malloc(10 * sizeof(struct _field));

Then you could access fields like before. Remember to free them up.
free(fields[0]);
free(fields[1]);
free(fields[2]);

