reinterpret_cast
From: Rich (Someone_at_somewhere.com)
Date: 12/29/04
- Next message: wil: "search text for a list of words...how?"
- Previous message: Mike Wahler: "Re: C/C++ Users Journal"
- Next in thread: B. v Ingen Schenau: "Re: reinterpret_cast"
- Reply: B. v Ingen Schenau: "Re: reinterpret_cast"
- Reply: Francis Glassborow: "Re: reinterpret_cast"
- Reply: Rich: "Re: reinterpret_cast"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 29 Dec 2004 17:44:20 +0000 (UTC)
Hello,
Sorry to be a pain, I had another cast question and have completed the
question correctly I think, have answered correctly, but not entiely sure.
The question is
Create a function that takes a pointer to an array of double and a value
indicating the size of the array.
The function should print each element in the array.
Now create an array of double and initialise each element to 0 then use
your function to print the elements of the array.
Next use reinterpret_cast to cast the starting address of the array to
an unsigned char* and set each byte of the array to 1( hint: you will
need to use sizeof to calculate the number of bytes in a double ). now
use the array printing function to print the results why do you think
each elemement was not set to 1.0?
Ok first off here is the function which I am certain I have correct :)
void PrintArray( double* dArray, int nElements )
{
for( int i = 0; i < nElements; i++ )
{
cout << dArray[ i ] << endl;
}
} ///:~
Now the main part of the program what I am not certain I have done
correctly :(
int main()
{
double dArray[ 10 ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
char pause = ' ';
double* p_dArray = &dArray[ 0 ];
PrintArray( p_dArray, 10 );
unsigned char* UC = reinterpret_cast<unsigned char*> ( &dArray[ 0 ] );
// cast to unsigned char* pointing to start address of array
for( int i = 0; i < ( 10 * sizeof(double) ) / ( sizeof( unsigned char )
) ; i++ )
{
UC[ i ] = '1';
}
PrintArray( p_dArray, 10 );
cin >> pause;
return 0;
} ///:~
in the for loop I used a calculation to divide number of bytes in a
double by number of bytes in an unsigned char, since the question says
set each byte of the array element to value 1 then there will be however
many bytes in a double more to set than if using a double, on my
computer an unsigned char is 1 byte a double is 8 bytes, so if there are
10 double elements in the array it will need to set 80 bytes to the
value of 1.
As for why it did not print out 1.0 for each element, this is becuase
there is a mantissa and exponent part to a double and since these are
being set to '1' then this has the effect on the entire value of the
double will be affected.
Sorry for the questions, but we haven't covered this yet and was an
excercise to find out about all the different casts including const_cast
and answer the questions.
Hopefully I have this question right, but if someone could confirm this
it would be useful
TIA
- Next message: wil: "search text for a list of words...how?"
- Previous message: Mike Wahler: "Re: C/C++ Users Journal"
- Next in thread: B. v Ingen Schenau: "Re: reinterpret_cast"
- Reply: B. v Ingen Schenau: "Re: reinterpret_cast"
- Reply: Francis Glassborow: "Re: reinterpret_cast"
- Reply: Rich: "Re: reinterpret_cast"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|