views:

97

answers:

1

Hello there, i have very low C++ experiences and i need little help in casting a type. I actually in a big dependence for that cast, need it rly hard.

So ...

I have this types :

typedef short DCTELEM;
typedef DCTELEM DCTBLOCK[64];

Array of last type and a pointer to a malloc'ed array of shorts:

DCTBLOCK MQUAD;
short * ptrArray;

I need MQUAD to bet to specific location pointed to by ptrArray;

In C i would prolly write something like

MQUAD = ptrArray + 3 * 2;

and have MQUAD after that pointing to a needed location, but i get

error C2440: '=' : cannot convert from 'short *' to 'DCTBLOCK'

in c++, cuz i know there is a difference in a type of array and a pointer to some-type.

Big thanks in advice.

+1  A: 

Your MQUAD variable is an array, not a pointer, so you can't assign to it (thanks to Remy Lebeau's comment). If you declare it as:

DCTELEM *MQUAD;

then you can assign to it:

MQUAD = reinterpret_cast<DCTELEM *>(ptrArray + 3 * 2); 

This is using the C++ cast syntax. You can use C cast syntax if you like too.

Greg Hewgill
MQUAD is an array variable, not a pointer variable. You cannot assign a pointer value to it. If Anton wants MQUAD to point at a location inside the malloc'ed shorts that are being pointed at by the ptrArray variable, then MQUAD has to be declared as a DCTELEM* or short* pointer to begin with.
Remy Lebeau - TeamB
@Remy Lebeau: Indeed, quite right. I'll update my answer.
Greg Hewgill
Yes, totally rigth, i just realized, 1 mins before openning that window :).
Anton