I was training C on this kata: https://www.codewars.com/kata/54d7660d2daf68c619000d95/c and got stuck by a strange signature of the function:
long long (*get_res(long long lst[][2], int row, long long LCD))[2]
Here is how I create and malloc my array:
long long (*get_res(long long lst[][2], int row, long long LCD))[2]
{
long long **res;
long long mul = 0;
res = (long long **)malloc(sizeof(long long *) * row);
if (!res)
return (NULL);
for (int i = 0; i < row; i++)
{
res[i] = (long long *)malloc(sizeof(long long) * 2);
mul = LCD / lst[i][1];
res[i][0] = lst[i][0] * mul;
res[i][1] = lst[i][1] * mul;
}
return ((long long(*)[2])res);
}
Then I return the result of this function in the main function like this:
return (get_res(lst, row, LCD));
And I get some strange results on the tests. I've already checked all the calculations: they work just as expected. Here's how the tester decided to get the result of the function (yet another point that I don't understand):
long long data[3][2] = { {1, 2}, {1, 3}, {1, 4} };
dotest(data, 3, "{{6, 12},{4, 12},{3, 12}}");
...
void dotest(long long a[][2], int row, char* sexpr)
{
long long* act = convertFrac(a, row);
char* sdat = array2D2StringInt(a, row, 2);
char* sact = array2D2StringInt((long long(*)[2])act, row, 2);
//some checking
}
So here are the questions:
1. What should I do to make the program work as expected?
2. Why would you opt for this signature instead of a classic
type **Func_name(params)
?