Monday, 12 August 2013

Casting a void pointer to an array of short

Casting a void pointer to an array of short

I have a void pointer data that I happen to know points to an array of
1048576 unsigned 16 bit integers (unsigned short). I am successfully able
to write them to file as follows:
FILE *pFile;
pFile = fopen( "data.raw", "wb" );
size_t readoutstride = 2097152;
if( pFile )
{
err = fwrite( data, 1,readoutstride , pFile );
fclose( pFile );
}
I would like to be able to access individual elements. My current approach
is as follows:
unsigned short (*Array)[1048576] = (unsigned short (*)[1048576])data;
unsigned short FirstElement = *Array[0];
unsigned short SecondElement = *Array[1];
unsigned short ThirdElement = *Array[2];
I happen to know what values to expect (because I can write to file). The
numbers appear jumbled, to say the least.
Array[0] gives me the expected value
Array[1] gives me zero (not expected value)
Array[2] gives me what I expected for Array[1]
I'm missing something obvious here but I can't pinpoint it. Any thoughts?

No comments:

Post a Comment