Re: problem with cast and unions
- From: mohangupta13 <mohangupta13@xxxxxxxxx>
- Date: Thu, 20 Aug 2009 08:38:47 -0700 (PDT)
On Aug 20, 6:19 pm, Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:
torri <vincent.to...@xxxxxxxxx> writes:is it also valid if the memeber c here would have been something else
On Thu, 20 Aug 2009 13:37:41 +0100, Ben Bacarisse wrote:
<corrected in another message to:>
Str s = ((Type)f).s;
the purpose is to get a type of the union from another of its type
Ah. Well that all depends now. Doing so is sufficiently odd that I
can't suggest a best way. It could go horribly wrong in all sorts of
ways. What is the real purpose? I.e. why are you trug to do this?
There may be a better way to get the overall end result.
Ok. First, my aim is to make a library compiling with suncc. That library
is designed to interpret limited Small programs (Small is a language).
Think of it as a compiler for Small programs. Note that I didn't write
that library. I just try to compile on OpenSolaris.
My guess would have been a language implementation.
The part where there is that problem is about conversion from a C float
to a Small "cell" (actually an int).
Here are the used types:
typedef int Embryo_Cell;
typedef union
{
float f;
Embryo_Cell c;
} Embryo_Float_Cell;
/** Float to Embryo_Cell */
#define EMBRYO_FLOAT_TO_CELL(f) ((Embryo_Float_Cell) f).c
and an example of use:
static Embryo_Cell
_embryo_fp(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
{
/* params[1] = long value to convert to a float */
float f;
if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
f = (float)params[1];
return EMBRYO_FLOAT_TO_CELL(f);
}
Note that the macro EMBRYO_FLOAT_TO_CELL is used a lot of time in that
part of program.
I hope that it is clearer, now :-)
Yes, but I don't see why you would try to hide the problem in a
macro. I'd write the example as:
static Embryo_Cell
_embryo_fp(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
{
/* params[1] = long value to convert to a float */
Embryo_Float_Cell efc;
if (params[0] != (1 * sizeof(Embryo_Cell))) return 0;
efc.f = (float)params[1];
return efc.c;
}
but I'd also want to look at the (float)params[1] part (it may be fine
you don't give the Embryo_Cell type).
If you can't re-write to avoid all this messing about, you can convert
via a pointer:
#define EMBRYO_FLOAT_TO_CELL(f) (((Embryo_Float_Cell *)&(f))->c)
(i mean say a struct) then just being a int .(through the typedef
thing)??
can anyone please tell me how is the structure assignment implemented
i mean is the copying done byte-wise or member-wise??
but there is a lot of finger-crossing going on in all these cases.
--
Ben.
.
- Follow-Ups:
- Re: problem with cast and unions
- From: Ben Bacarisse
- Re: problem with cast and unions
- References:
- problem with cast and unions
- From: torri
- Re: problem with cast and unions
- From: Ben Bacarisse
- Re: problem with cast and unions
- From: torri
- Re: problem with cast and unions
- From: Ben Bacarisse
- Re: problem with cast and unions
- From: torri
- Re: problem with cast and unions
- From: Ben Bacarisse
- problem with cast and unions
- Prev by Date: Re: detabbing again
- Next by Date: Re: detabbing again
- Previous by thread: Re: problem with cast and unions
- Next by thread: Re: problem with cast and unions
- Index(es):
Relevant Pages
|