Why do we need to pass a 2d array in this form: a [ ] [1000], and not just simply a [ ] [ ], like in the spiral_print function in the video?
For one dimensional array we just pass it as a[ ].
Why do we need to pass a 2d array in this form: a [ ] [1000], and not just simply a [ ] [ ], like in the spiral_print function in the video?
For one dimensional array we just pass it as a[ ].
Why we don’t need to pass any dimensions for 1D array ?
eg void fun(int a[], int n)
Ans - the compiler can calculate address associated with any array element (a[i]) without any dimension information of the array
eg. addr a[i] = (addr of a) * i* (size of int)
Why we need to pass atleast 2nd dimension for 2D array during function?
eg void fun( int a[][n], int m, int n)
Ans: to calculate address for say a[i][j];
addr of a[i][j] = addr of a + (i) * (no of cols) * (size of int) + j*(size of int) … if the array was stored in memory as row by row.
Hence we need atleast the ‘no of cols’ information to accurately calculate the address associated with any 2D array element.
You can refer this https://www.geeksforgeeks.org/pass-2d-array-parameter-c/
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.