Re: pointer/ref question

From: Leor Zolman (leor_at_bdsoft.com)
Date: 02/25/04


Date: Wed, 25 Feb 2004 13:46:02 GMT

On 24 Feb 2004 22:15:02 -0800, wft2vicki@earthlink.net (Vicki) wrote:

>Hi,
>
> Can someone help me with the following code? I don't understand why
>the & (reference...is it called specifier?) is needed with the
>pointer.
>
>function header:
>
>void insertAfter(List Cell*& p, int k, int x);
>
>I thought that with the pointer, it is already getting passed by
>reference

You've already gotten good responses that illustrate the basic picture.
Part of the problem here, IMO, is terminology, and it would seem that
there's quite a bit of confusion over the meaning of the term "pass by
reference". Those that have been arguing the point (no pun intended) for
years seem to have settled on an interpretation of that term that they try
to disassociate with anything to do with "pointers" in C or C++, but I
think that is a losing battle in practical terms. I use the term "pass by
reference" to generically indicate we're conveying the address of an
object. Thus, to /me/ one can describe both of these situations as "passing
x by reference" in C or C++, while the second would only qualify as such
in C++ (as there's no reference type in C). I'm not adverse to also using
the term "pass by pointer" for the first case when teaching C++, in order
to have a separate term for that and passing to a function taking a
parameter by "real" reference:

void f(int *v) {...};
void g(int &v) {...};
int main()
{
        int x;
        f(&x); // C and C++
        g(x); // C++ only
}
        
The key is always use the term "by reference" in context: /What/ are you
passing by reference? In the call to f, I'm passing x by reference, but I'm
passing a pointer to x by value. I'd be doing exactly the same thing if
I'd created a pointer variable initialized to &x and passed /that/ by
value.

I've gotten somewhat flamed for using the terms this way in the past, and
it'll probably happen again in the future (perhaps Real Soon Now), but at
least my point of view represents a framework where it is possible to
describe most, if not all, kinds of argument passing in C and C++ with a
minimal number of ambiguous terms...
        -leor

>, so why the needed & operator. Isn't this like sending the
>address twice?
>
>What am I missing?
>Vicki

Leor Zolman
BD Software
leor@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
           Decryptor at www.bdsoft.com/tools/stlfilt.html



Relevant Pages

  • Re: ByVal vs. ByRef
    ... But I still don't understand why I have to pass the TreeNode argument ByVal. ... I understand why passing it ByVal works. ... What is "the Value" of a reference variable? ... A "pointer" to the object. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to pass a reference to a CComBSTR
    ... >> This is not passing by reference - you are passing pointers. ... CComBSTR tempCComBSTR); ... Approach B (passing by pointer) ...
    (microsoft.public.vc.atl)
  • Re: using const & in function prototypes
    ... BOOL func; ... You are passing a reference to an object instead of a copy. ... passing a pointer is at best as performant as passing a DWORD, ...
    (microsoft.public.vc.language)
  • Re: no pointer in Java => my problem
    ... > the value you are passing is an adress and you can't change the ... When passing a reference, you don't get ... access to the pointer. ... Neither Java nor C have anything resembling this. ...
    (comp.lang.java.programmer)
  • Re: [PATCH] [0/9] Use 64bit x86 machine check code for 32bit too
    ... warning: passing argument 2 of ‘strict_strtoull’ makes integer from pointer without a cast ...
    (Linux-Kernel)