comp.lang.c
[Top] [All Lists]

Re: Union trouble

Subject: Re: Union trouble
From: "Bartc"
Date: Fri, 28 Mar 2008 22:54:12 GMT
Newsgroups: comp.lang.c


"David Mathog" <mathog@xxxxxxxxxxx> wrote in message 
news:fsj71l$1hl$1@xxxxxxxxxxxxxxxxxxx
> Bartc wrote:
>> I'm porting some code from a language that allows casual aliases to 
>> struct fields and between variables.
>>
>> I can duplicate some of this with unions but the results look inelegant. 
>> The first example involves structs:
>
> By "casual aliases" do you mean that it sets two variables "one" and "two" 
> to both reference the same storage area "one_two_data"?

Yes. Although the types could be different.

> Rather than using Union it may be easier to just code all of these aliases 
> into your program explicitly, like:
>
> int one_two_data;
> int *one = &one_two_data;
> int *two = &one_two_data;
>
> If you don't want to have to write "*one" everywhere then you can
> use instead:
>
> int *fake_one = &one_two_data;
> int *fake_two = &one_two_data;
> #define  one (*fake_one)
> #define  two (*fake_two)
>
> then use "one" and "two" as the variables. So long as all the casual 
> aliases are to identical data types at that storage location this should 
> work fine.

I suppose using pointers to the same memory was always an option, if I 
couldn't avoid the extra dereference step. But at least it makes it 
possible. I will try out your idea.

-- 
Bart




<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy