Home > Blockchain >  Can I use a matrix in a function without specifying ROWS?
Can I use a matrix in a function without specifying ROWS?

Time:11-13

While writing a rather simple code for an exercise i was assigned, I run into this problem. The exercise asks me to create a function (what it does it's not important, but it loops through each element of the matrix to do something) getting as parameters a 2D array (NxM) and a double (which is used in the function). The function so looks something like this:

int function(int (*myarray)[M], double y){}

Now if I'm not missing anything, this is not enough because I can't loop through the elements without using the value N as range. Am I right? If there is no way to use the number of rows inside this function without passing it as a parameter, it means that the exercise took for granted that passing the matrix meant also passing N (rows) as a parameter.

(I thought about using sizeof to get the number of rows somehow, but sizeof(myarray) inside function(){} doesn't work as expected. What is the difference between myarray inside this function and the original myarray I declared in main()?)

myarray is declared as int myarray[N][M]; and then initialized with a loop which contains myarray[i][j] = rand()%(max-min 1) min;. I don't think this is too relevant to the problem but I include it to explain it is automatically allocated.

I already checked some similar discussions like thisand this but since everyone gives the dimension as parameter I didn't find an answer yet.

CodePudding user response:

Can I use a matrix in a function without specifying ROWS?”

The answer is largely no. The function must have some way of knowing how much data to use from the array. That is not necessarily the original size of the array; it could be the amount of the array, or portions of the array, the caller wants the function to operate on. The C standard does not provide any way to know the original size of an array from a pointer.

That information can be made available to the function in multiple ways: By a parameter, by an external variable, including by a sentinel value.

Now if I'm not missing anything, this is not enough because I can't loop through the elements without using the value N as range. Am I right?

Passing the number of rows in the array is one way of being able to iterate through them. The information could be made available in other ways. In your circumstance, passing the number of rows is likely the intended method.

What is the difference between myarray inside this function and the original myarray I declared in main()?)

When a function parameter is declared as an array, it is automatically adjusted to be a pointer. So, if a parameter were declared as int myarray[N][M], it would be automatically adjusted to int (*myarray)[M]. Then myarray inside the function is not an array; it is a pointer.

When an array is passed to a function as an argument, it is automatically converted to a pointer to its first element. This automatic conversion occurs whenever an array is used in an expression except as the operand of sizeof or the operand of unary & or when it is a string literal that is used to initialize an array.

  • Related