First of all, for any pointer or array p
and index i
, the expression *(p + i)
is exactly equal to p[i]
. That means you can use drevo[i]
in all places where you use *(drevo + i)
.
While the above won't solve your problem, it might give you an idea what the problem is if you think of drevo
as a pointer to an array of other pointer to arrays, similar to an array of arrays.
So in the loop when you use
*drevo + i) = (int *) malloc(10000 * sizeof(int));
scanf("%d", *(drevo + i));
it is equal to
drevo[i] = malloc(10000 * sizeof(int));
scanf("%d", drevo[i]);
That code will not be correct, as you will read into drevo[i][0]
always.
However, that is still not the cause of the crash, it's just faulty logic.
The problem is probably this:
scanf("%d", *(drevo + 2));
It is equal to
scanf("%d", drevo[2]);
And that will read into drevo[2][0]
. But in the very first iteration of the outer loop, when i == 0
, then the pointer drevo[2]
is uninitialized! It doesn't point anywhere valid, and by dereferencing that pointer you will have undefined behavior and very likely the crash you are experiencing.
Try to convert all *(drevo + i)
and similar expressions into drevo[i]
and see if they all make sense. It is, by the way, a few less characters to write. Other than that, you should learn how to debug your programs. We can not really help you more, since you don't tell us what your program is supposed to do, and with the flawed and faulty logic (and undefined behavior) it's hard for use to try and figure it out ourselves.