My code is:
//fun FUNCTION
void fun(int D1, int D2, int arr[D1][D2])
{
//FUNCTION BODY
}
//MAIN FUNCTION
int D1, D2;
int arr[][3]=//VALUES
D2=sizeof(arr[0])/sizeof(a[0][0]);
D1=sizeof(arr)/sizeof(a[0]);
fun(D1,D2,arr);
How would compiler know the dimensions of array arr
in function fun if the order of parameters of function is uncertain?
The compiler has to generate the code to create the space for the frame on the stack to hold the array and other local local variables. For this it needs the size of the array. Show activity on this post.
If you don't specify a size of the array, then it doesn't know how much space to set aside for the array elements. If you create just a pointer to an array, then all you need to do is allocate the space for the pointer itself, and then you can dynamically create array elements during run time.
The simplest way to think of it is that a dimension of an array is the number of square brackets that follow the type: int[] is a single dimension array, int[][] is a 2 dimensional array, etc. Sometimes it is helpful if you think of an array as if you were graphing them in multiple dimensions. A 1-d array is simply a line and has 1 axis in a graph.
Here array contains element 5 and its dimension is 0. Here we give an example to create a one-dimensional array: The given example output is: Here the given example dimension is 1. Here we give an example to create a two-dimensional array:
The C standard does not specify the order of evaluation of arguments. However, all arguments are evaluated before the function is called. Per C 2018 6.5.2.2 10 (which discusses function calls):
There is a sequence point after the evaluations of the function designator and the actual arguments but before the actual call.
Thus, when execution of the called function begins, all arguments have been evaluated, so their values are known. Then the size expressions of variably modified parameters are evaluated, per 6.9.1 10 (which discusses function definitions):
On entry to the function, the size expressions of each variably modified parameter are evaluated and the value of each argument expression is converted to the type of the corresponding parameter as if by assignment.
(Note that arguments are what a caller passes to a function, and parameters are the objects declared as part of the function declaration or definition that acquire values on entry to the function.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With