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!

4
  • This stackoverflow.com/questions/34134074/… has not helped me btw Commented Apr 18, 2021 at 3:10
  • Passing in arrays can be problematic because of 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. Commented Apr 18, 2021 at 3:13
  • Hmmm not sure of any other way to do it :( Commented Apr 18, 2021 at 3:15
  • I'm kind of baffled you don't know the size here when your signature literally says 64. Commented Apr 18, 2021 at 3:25

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude. I figured it out but thank you anyway +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.