0

Currently i'm tring to do 2d array in c++ that can have diffrent second column size. This is my code:

int **testZadanie;
testZadanie = new int*[testy];
...
for (int i = 0; i<testy; i++)
{
    cin >> liczbaLakomczuchow >> liczbaCiastek;
    testZadanie[i] = new int[liczbaLakomczuchow];
    for (int j = 0; j<liczbaLakomczuchow; j++)
    {
        cin >> czasJedzenia;
        //testZadanie[i, j] = czasJedzenia; this not works
        *testZadanie[i, j] = czasJedzenia;
    }
}

But when I want to attach values for each column i got exception. So i'm not sure if I can do this what i actually trying do ? ( declaring dynamic column element for each row and attach for each item in row ? )

1 Answer 1

1

Use x[y][z] instead of x[y, z].

When you write *x[y, z], the compiler interprets it as x[z][0] (which is not what you want) because:

  • a, b is a comma operator, which evaluates both operands and returns the value of the second one.
  • *a is equivalent to a[0].

This answer generally applies to builtin types only. You can overload ,, * and [] operators for a class, changing their behaviour completely.

If you don't know what operator overloading is, just ignore this remark.

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

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.