Arc Official
Arc Official

Reputation: 11

Get row of 2d multi dimensional array in C

int loadRainData(FILE *fp, char sites[6], double rf[64][6]) {
                    

int row = sizeof(rf) / sizeof(rf[0]);


printf("Number of rows: %d\n", row);


return row;
}

I dont understand how to get the amount of rows in my function. There is 7(right now but this is dynamic) values in rf[this guy][6]; I am trying to find how many values there are. Thank you for your help!

Upvotes: 1

Views: 183

Answers (1)

David Ranieri
David Ranieri

Reputation: 41017

An array "decays" into a pointer to the first element when passed to a function.

Check the C-FAQ:

For a two-dimensional array like

int array[NROWS][NCOLUMNS];

a reference to array has type ``pointer to array of NCOLUMNS ints,''

in consequence

int loadRainData(FILE *fp, char sites[6], double rf[64][6])

and

int loadRainData(FILE *fp, char sites[6], double (*rf)[6])

are the same.

You need to pass the number of rows to the function, so the prototype should be something like:

int loadRainData(FILE *fp, char sites[6], size_t rows, double (*rf)[6]);

Notice the use of size_t instead of int (take a look to What is the correct type for array indexes in C?)

Upvotes: 3

Related Questions