1
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!

  • This https://stackoverflow.com/questions/34134074/c-size-of-two-dimensional-array has not helped me btw – Arc Official Apr 18 '21 at 03:10
  • Passing in arrays can be problematic because of [array to pointer decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay). Don't use `sizeof` to guess how many rows there are, pass that in as an explicit argument. This must be done at the caller, this function does not have the information it needs. – tadman Apr 18 '21 at 03:13
  • Hmmm not sure of any other way to do it :( – Arc Official Apr 18 '21 at 03:15
  • I'm kind of baffled you don't know the size here when your signature *literally* says 64. – tadman Apr 18 '21 at 03:25

1 Answers1

3

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?)

David Ranieri
  • 39,972
  • 7
  • 52
  • 94