&target

This is a discussion on &target within the C Programming forums, part of the General Programming Boards category; I am reading the book in Operating System Concept, Can someone told me what is the meaning of &target ? ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    &target

    I am reading the book in Operating System Concept,
    Can someone told me what is the meaning of &target ?

    Code:
    boolean TestAndSet(boolean &target) {
               boolean rv = target;
               target = true;
               return rv;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The code you're looking at is C++ code. You do not use the & operator that way in C. In the above code, the & means that 'target' is a reference. References are similar to pointers, except that you don't need to dereference them like you do pointers in C. They also differ in that they can never be NULL. That is to say, a pointer can be NULL, references cannot. Their calling convention is different than with pointer also, but in effect, they're the lazy man's pointer.
    Code:
    void foo( int &ref )
    {
        ref = 10; /* Set the value of the passed variable to 10. */
    }
    
    void pointy( int *ptr )
    {
        *ptr = 10;
    }
    
    ...
    
    int x;
    
    ref( x ); /* Called via reference. */
    pointy( &x ); /* Called using pointer. */
    The two functions preform the same task.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    References are also useful in obfuscated code contests, since functions returning a reference can be used as an lvalue, ie: foo() = 3;

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21