Re: better understanding references

From: jeffc (nobody_at_nowhere.com)
Date: 03/04/04


Date: Thu, 4 Mar 2004 17:54:24 -0500


"thides" <swatts@globalserve.net> wrote in message
news:yCN1c.69745$bJ6.19904@nntp-post.primus.ca...
>
> "jeffc" <nobody@nowhere.com> wrote in message
> news:40475820_3@news1.prserv.net...
> >
> > "thides" <swatts@globalserve.net> wrote in message
> > news:bWy1c.68187$lx2.42640@nntp-post.primus.ca...
> > >
> > > When is & (address of ) not used to take an address?
> >
> > When you're using it in the callee to mean "pass by reference" :-)
>
> Yes but in the sort example my understanding is 'sort(int &i, int &j)'
> accepts the address of what it is passed as oppose to the value. Its not
> that we are dealing with the value but the address of the value of what is
> passed. And as was pointed out in the FAQ or in one of the responses to
the
> original question the compiler does the dereferencing automatically so we
> dont have to mess around with pointers. & is always related to addresses
of
> some type or meaning except used with binary or logical operators.
Right?

Well, probably, but that is not the way you're supposed to think. When you
push your gas pedal, you want your engine to speed up. You don't care if
that happens with a carburetor or fuel injection. Likewise, in the function
declaration you showed, there is no reason to think of addresses at all, and
I'm sure the designer of the feature did not want you to think in those
terms either. You should think only in terms of "pass by reference".
Whether this happens with taking the address or not is none of our concern.
Let's say someone wants to call sort. Should the programmer think about how
to get the addresses of his variables in there? Definitely not. He should
just call it like this:
int x;
int y;
scan(x, y);
That's it. "pass by reference", not "pass by value". "Address" never
needs to be considered.



Relevant Pages

  • Re: better understanding references
    ... accepts the address of what it is passed as oppose to the value. ... that we are dealing with the value but the address of the value of what is ... dont have to mess around with pointers. ... some type or meaning except used with binary or logical operators. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: A taxonomy of types
    ... I am describing how to represent the representation (and, ... if the system follows static typing rules (such as in a compiler), ... so, the hardware sees pointers and pointer arithmetic, but the compiler ... "Besides the char types, up to three sizes of integer, declared short int, ...
    (comp.lang.misc)
  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • I want my segmentation fault!
    ... no occurrences of free and a lot of routines returning pointers to ... the pointer returned by the allocator (either directly or as a component ... int length_of_list; ...
    (comp.lang.c.moderated)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)