Re: locating an array
On Feb 17, 5:26 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
James Kanze <james.ka...@gmail.com> writes:
I have a structure composedx of floats and strings. I want a
dummy array that is the same length starting at the same
address. I do this so I cann access the individual bytes of
the structure.
#include <iostream>
#include <ostream>
struct s { float a, b, c; };
struct t { char a[ sizeof( s )]; };
int main()
{ s s0;
t * t0( reinterpret_cast< t* >( &s0 ));
::std::cout << sizeof( t0->a ); }
This seems more complicated than necessary, as well as
unnecissarily introducing undefined behavior.
Where does it introduce undefined behavior?
When you access t0. T0 doesn't have a character type, so any
access of s through it is undefined behavior. (I think that
there are certain cases with g++ where this actually doesn't
work. But it's actually an interesting question---t->[i] is a
character type, and while I think that taken literally, the
standard says this is undefined behavior, I have a hard time
imagining an implementation where it wouldn't work, but
accessing the results of a reinterpret_cast< char* >( &s0)
would.)
If the goal is just to look at the individual bytes in an
object:
The OP wanted to have an =BBarray that is the
same length starting at the same address=AB.
Why? What does he really mean by array? For better or for
worse (mostly worse), getting an "unsigned char*" to the first
byte and knowing the size of the type (sizeof) comes down to
pretty much the same thing.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34