Florian Schneider
Florian Schneider

Reputation: 39

C++: Get 1D array from Multidimensional array

I have this defined:

int zinc[] =        {D3,         180,        30,         5,          0,          15};
int omega[] =       {D2,         180,        40,         0,          0,          30};
int magnesium[] =   {D1,         180,        30,         5,          0,          15};



int order[] = {*zinc, *omega, *magnesium};

calling

dispense(zinc);

works, calling

dispense(order[1]);

does not.

I think I am missing only one char.

Thanks in advance!

Upvotes: 1

Views: 70

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

In this call

dispense(zinc);

the argument expression has an array type.

In this call

dispense(order[1]);

the argument expression has the type int.

So argument expressions have different incompatible types.

It seems you mean the following declaration

int * order[] = { zinc, omega, magnesium};

instead of that

int order[] = {*zinc, *omega, *magnesium};

In this case these two calls

dispense(zinc);

and

dispense(order[1]);

will be identical provided that the function does not accept an array by reference.

In any case you have no multi-dimensional array. In the above declaration you have an array of pointers to first elements of the arrays used as initializers. Nevertheless you can access integer objects stored in arrays applying two subscript operators as for example

order[i][j]

Upvotes: 2

Related Questions