-3

I have a 2D array called drevo. I have located both dimensions of memory, so why does it throw this exception?

int main() {
    int n;
    int k;
    int stukaza;
    int parameter;

    scanf("%d", &n);

    int **drevo = (int **) malloc(n * sizeof(int *));

    for (int i = 0; i < n; i++) {
        *(drevo + i) = (int *) malloc(10000 * sizeof(int));
        scanf("%d", *(drevo + i));

        for (int j = 1; j < (**(drevo + i)) + 1; i++) {

            printf("%d jlk", *(drevo));
            scanf("%d", *(drevo + 2));


        }
    }
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
Nejc Ahtik
  • 43
  • 5
  • 2
    What input are you giving the program? And please use the syntax `drevo[i]` instead of `*(drevo+i)` as it's much more readable. `(**(drevo+i))` is concerning. – Kevin May 14 '18 at 19:43
  • 2
    Please take notice of compiler warnings: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'int *' – Weather Vane May 14 '18 at 19:44
  • What is the purpose of your embedded for-loop? It's not making use of either counting vars. Also, `*(drevo)` is of type `int *` and you are printing it as if it is of type `int`. – Christian Gibbons May 14 '18 at 19:45
  • 1
    [do i cast the return value of `malloc` in c?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Tormund Giantsbane May 14 '18 at 19:45

2 Answers2

3

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2
for (int j = 1; j < (**(drevo + i)) + 1; i++)

I think you want to increment j here not i, but it is a little bit unclear what you want to do.

Osiris
  • 2,783
  • 9
  • 17