243 views

Let arrays OneD and TwoD are declared as follows as:

int OneD[10];
int TwoD[4][5];


Which of the following is/are valid syntax to pass OneD and TwoD to some function fun()?

A syntax is valid if it gets compiled without any warning or error.

1. void fun(int OneD[10]);
2. void fun(int OneD[5]);
3. void fun(int TwoD[][5]);
4. void fun(int **TwoD);

typo in options: ‘voin’
Fixed.

Option A, B is correct it doesn’t matter what size you mention because what is being passed is the pointer pointing to the initial address of the array.

(Source: C Programming Language by Dennis Ritchie)

Option C is correct, option D is wrong because while passing 2D array to the function we have to mention number of columns each row have.

Internally compiler will treat it as

void fun(int (*TwoD)[5]);

(Source: C Programming Language by Dennis Ritchie)

by

in the question, what does this mean “valid syntax to pass OneD and TwoD”?
does it mean we need a function that could handle both of these types of arrays as parameter?

@Shubhamishere not both it should accept one of the given array as parameter.

while passing 2D array to the function we have to mention number of rows each column have.

I did not understand this line. Is there a typo? Because we are mentioning no. of columns here in these options A,B,C

@Shubhamishere yes thanks for pointing out :)

1 vote