0

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. :)

ChrisD
  • 674
  • 6
  • 15
wis.niowy
  • 87
  • 1
  • 12
  • 3
    What is the problem? Any kind of error you are getting? – Ashalynd Dec 28 '15 at 10:56
  • 1
    Welcome to Stack Overflow! [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Dec 28 '15 at 10:56
  • To begin with, what does the `ini` function return? I mean, what is it's return type? To continue you can only initialize an array by using "a brace-enclosed initializer list", no other way, and you can't *assign* an array later. To further continue, what the `init` function tries to do is return a pointer to a local variable, which will lead to *undefined behavior*. To end, the expression `*(tablica+i)` is equivalent to `tablica[i]`, a syntax that is easier to read and understand. – Some programmer dude Dec 28 '15 at 11:00
  • `tab == NULL` : `tab` never become `NULL`. also `tab` is local variable. – BLUEPIXY Dec 28 '15 at 11:05
  • 1
    You're trying to dynamically initialize `tablica` in main by calling `ini`. To do that, you need to pass it to `ini` as a parameter. – Tom Karzes Dec 28 '15 at 11:14

2 Answers2

1

A couple of obvious things:

  • you return a locally declared array (int *tab[N];) outside of its scope, therefore it's going to be garbage there. This must be your most visible problem.

  • you malloc the elements of that array inside your ini() method but never release them, therefore you are getting a memory leak.

  • checking tab for NULL does not make much sense, because tab would never be NULL; tab[i], on the other hand, can be NULL and could be checked for that after malloc.

  • you don't check that your tablica is not -1.

Ashalynd
  • 12,363
  • 2
  • 34
  • 37
0

I wanted to upgrade that program (and to make it work in the same way):

int ini()
{
int *tab;
int i, j, a, b;
for (i = 0; i < N; i++)
tab = (int*)malloc(N*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 + N*i +j ) = rand() % (b - a + 1) + a;
    }
}
return tab;
}


int main()
{
int i, j, *tablica = ini();
for (i = 0; i < N; i++)
{
    for (j = 0; j < M; j++)
    {
        printf("%d  ", *(tablica + N*i + j));
    }
    printf("\n");
}
system("PAUSE");
return 0;
}
wis.niowy
  • 87
  • 1
  • 12