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

5
  • 3
    What is the problem? Any kind of error you are getting? Commented Dec 28, 2015 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.. Commented Dec 28, 2015 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. Commented Dec 28, 2015 at 11:00
  • tab == NULL : tab never become NULL. also tab is local variable. Commented Dec 28, 2015 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. Commented Dec 28, 2015 at 11:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.