This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Can something similar to &x=&y be accomplished in C
Thanks, I was more concerned with performance (an extra op or two each
access), than with ease of use.
-Jim Stapleton
On 8/29/07, Tom St Denis <tstdenis@ellipticsemi.com> wrote:
> Jim Stapleton wrote:
> > Lets say I'm iterating through an array, and I have a lot of
> > operations on the array needed to alter it...
> >
> > int p = {1, 2,3,4 ...};
> > for(int i = 0; i < length_of_p; i++)
> > {
> > some_op(p+i);
> > }
> >
> > void some_op(int *t)
> > {
> > /*do stuff with/to t*/
> > }
> >
> > In C++ I could simply do int &t in the function, and it'd be fine, but
> > in C I have to use a pointer, which means a lot of dereferencing. Is
> > there any way to efficiently have the same effect as &x = &y (having a
> > variable "be" another variable, rather than just point to it) in plain
> > C with GCC, or is that out of the question?
> >
>
> You could always use define macros, e.g.
>
> void somefunc(int *pX)
> {
> #define X *pX
>
> X *= 3; X += 4; X whatever ...;
>
> #undef X
> }
>
> But I would recommend against that as it makes the code harder to read
> in the long run (imagine having 12 parameters + 15 locals + structs from
> various header files + etc).
>
> Tom
>