Thread: &target

  1. #1
    Registered User
    Join Date
    Mar 2004


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

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

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    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.
    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.

    Hope is the first step on the road to disappointment.

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Sydney, Australia
    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