
Structure Pointer Dereferencing
Quote:
> Is it a good practice to *strictly* avoid dereferencing of structure
> pointers inside your code ?
No. Who gave you that idea? There is nothing wrong with
dereferencing structure pointers.
Quote:
> I recently came across this neat trick to do the same.
> To access the fields of structures, I use the following macros:
> #define V(a) ((void)0,(a))
I suppose you're using this to transform an lvalue into an
rvalue? You might have noted so, because otherwise it's a
useless no-op.
Quote:
> #define dereferencer_1(a) (a)
This macro, however, seems truly pointless.
Quote:
> // following macro used to set fields
> #define x_f_1_foo_1(a) (dereferencer_1(a)->f_1)
So this is an lvalue that can be used to modify a member.
Quote:
> // to read a field
> #define f_1_foo_1(a) V(x_f_1_foo_1(a))
And this is an rvalue that cannot. Very clever.
Quote:
> Can any one point out the flaws in this approach? Any
> ideas/alternatives to do the same?
The main flaw is that in general the complication isn't worth
it. A secondary flaw is that the names are unreadable.
--
"It would be a much better example of undefined behavior
if the behavior were undefined."
--Michael Rubenstein
--