0

Why this code is giving so much error in passing 2D array ( 3rd function ) .

https://code.sololearn.com/c76vjwChECDi/?ref=app

28th Mar 2022, 1:16 PM
Abhay mishra
Abhay mishra - avatar
2 Answers
+ 5
You are facing the probably most irritating and difficult aspect of C. Pointers are not arrays, arrays are not pointers. They share an intimate relation, yes, but in some aspects they are not the same. So, mind your types! You func2 ecpects a pointer to int. An array decays to a pointer when passed to a function. But you are passing a pointer to an array. The type you pass in is int(*)[5], that is a pointer to a type that is 5 ints long. If you were to increment that pointer by 1, it would hop 5 ints forwards. But the argument type is int*. A pointer to just a single int. Those are distictively different types. For func3 similarly. The function expects a pointer to int. You pass a pointer to a two-dim array. Even if you removed the address operator, it would still be a pointer to an array of three ints. That is not the same as a pointer to pointer, or int**. That is not a two-dim array. Your correct parameter declaration would seem to be "int *two[3]". But that would be an array of three int pointers.
28th Mar 2022, 2:46 PM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 5
Continuing ... Effectively the same as int **two, which, as mentioned before, is not the correct type. You need a pointer to a type of three ints, because the array is laid out conecutively in memory. So, the correct type for func3 is "int (*two)[3]", and then pass only the array, not a pointer to it. Alternatively, use "int two[][3]"
28th Mar 2022, 2:48 PM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar