I'm just a beginner and have encountered a problem with an array of pointers. Could you show me where the mistake is?
int ini()
{
int *tab[N];
int i, j, a, b;
for (i = 0; i < N; i++)
{
tab[i] = (int*)malloc(M*sizeof(int));
}
if (tab == NULL)
return -1;
scanf_s("%d %d", &a, &b);
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
*(*(tab+i)+j) = rand() % (b - a + 1) + a;
}
}
return tab;
}
int main()
{
int i, j, *tablica[N] = ini();
for (i = 0; i < N; i++)
{
for (j = 0; j < M; j++)
{
printf("%d ", *(*(tablica+i) + j));
}
printf("\n");
}
system("PAUSE");
return 0;
}
The task itsef is simple and I can do it in the other way, but I just wanted to use this:
*(*(tab+i)+j)
instead of that:
*(tab + N*i + j)
since the second option wouldn't always work.
I'll be glad if you could give me a hand. :)