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; }
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; }
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.The two functions preform the same task.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. */
Quzah.
Hope is the first step on the road to disappointment.
References are also useful in obfuscated code contests, since functions returning a reference can be used as an lvalue, ie: foo() = 3;