I have this pointer to pointer function that takes two arguments and it is suppose to return pointer to the array. But what is the proper way to return pointer to the array in this case? Here is my code:
#include <stdio.h>
#include <stdlib.h>
int **multiTable (unsigned int xs, unsigned int ys)
{
unsigned int i, j;
int **table = malloc( ys * sizeof(*table));
// should I put malloc(ys * xs * sizeof(*table))?
for(i = 0; i < ys; i++)
{
for(j = 0; j < xs; j++)
{
table[i][j] = (j+1) * (i+1);
}
}
return table; //what would be the proper way to return table? Is this ok?
free(**table); //by the way, do I need this?
}
int main()
{
int sum = 0;
sum = multiTable2(3,2);
return 0;
}
what would be the proper way to return table? Is what I written the return table; ok or should it be like return *table or even return **table? And also, do I need to free the table?
table, but you don't allocatetable[x], leading to *undefined behavior, which you also have in thatfreecall.mainfunction is wrong as well. All in all, your compiler should be screaming at you!freeafter return.