Address-of operator (&) vs reference operator(&)

address operator in c in hindi
dereference operator
dereference operator c++
c++ pointers and references
what is pointer in c++
difference between address operator and pointer operator
pointer to pointer c++
dereference operator c++ example

I am kind confused about this case:

Declare a pointer:

int b =10;

Here & takes the address of b.

Consider another example:

/* Reference to the calling object can be returned */

Test& Test::func ()
   // Some processing
   return *this;

this should be a pointer, *this is the obeject pointed. But here we are asking to assign *this to &Test.

What should we modify the code to let the function return the address. Should we still use Test& ?

In C++ there're two different syntax units:

&variable; // extracts address of variable


Type& ref = variable; // creates reference called ref to variable

Easy usage example:

int v = 5;

cout << v << endl; // prints 5
cout << &v << endl; // prints address of v

int* p;
p = &v; // stores address of v into p (p is a pointer to int)

int& r = v;

cout << r << endl; // prints 5

r = 6;

cout << r << endl; // prints 6
cout << v << endl; // prints 6 too because r is a reference to v

As for using references in functions, you should google "passing by reference in C++", there're many tutorials about it.

Address-of Operator: &, The reference and dereference operators are thus complementary: & is the address-of operator, and can be read simply as "address of"; * is the dereference​  The address-of operator, applied to const or volatile objects, evaluates to const type * or volatile type *, where type is the type of the original object. When the address-of operator is applied to a qualified name, the result depends on whether the qualified-name specifies a static member.

Firstly, this is a pointer. The * dereferences the pointer, meaning return *this; returns the object, not a pointer to it.

Secondly, Test& is returning a reference to a Test instance. In your case, it is a reference to the object. To make it return a pointer, it should be Test*.

It makes more sense if you read a pointer declaration from right to left.

Test* func(); //When I call func, and dereference the returned value, it will be a Test

C pointer Address operators - IncludeHelp, The Address of Operator &. The & is a unary operator that returns the memory address of its operand. For example, if var is an integer variable, then &var is  Remarks. The AddressOf operator creates a delegate that points to the sub or function specified by procedurename. When the specified procedure is an instance method then the delegate refers to both the instance and the method. Then, when the delegate is invoked the specified method of the specified instance is called.

But here we are asking to assign *this to &Test.

No... you're asking for the value/expression *this to be used to return a Test&, which is a reference to a Test object. What that does is return a reference to the object on which func() is invoked.

What should we modify the code to let the function return the address. Should we still use Test& ?

You should use Test* instead... pointers are addresses, and having changed the return type you could return this (which is a pointer), but not *this because *this is not a pointer.

Pointers in C++ - Memory Addresses, The "Address Of" Operator denoted by the ampersand character (&), & is a unary operator, which returns the address of a variable. After declaration of a pointer variable, we need to initialize the pointer with the valid memory address; to get the memory address of a variable Address Of" (&) Operator is used. Actually, overloading unary operator& for a type pretty much makes generic programming impossible for that type, because the address of an object is too fundamental a property to play with naively. Most generic code assumes that applying & to an object of type T returns an object of type T* —you see, address-of is a fundamental concept.

The address and indirection operators in C Programming, In C++ there're two different syntax units: &variable; // extracts address of variable​. and. Type& ref = variable; // creates reference called ref to  The absolute address of program variables depends on a lot of factors. The C++ standard certainly doesn’t specify how variables are to be laid out in memory. Notice how the variable n is exactly 4 bytes from the first variable declared (start), which corresponds to the size of an int (4 bytes).

Pointers - C++ Tutorials, For any object (i.e, variable, array, union, struct, pointer or function) the unary address operator can be used to access the address of that object. Suppose that Address of Operator (&) The & is a unary operator in C which returns the memory address of the passed operand. This is also known as address of operator. Value of Operator (*) The * is a unary operator which returns the value of object pointer by a pointer variable. It is known as value of operator.

C++ Pointer Operators, Address of (or Referencing) operator (&): in c programming language - is used to get address Duration: 16:20 Posted: Apr 9, 2020 The & Operator (Address Of Operator) 1) The * Operator (Dereference Operator or Indirection Operator) "Dereference Operator" or "Indirection Operator denoted by asterisk character ( * ), * is a unary operator which performs two operations with the pointer (which is used for two purposes with the pointers).

  • Make it return a pointer.
  • Pay attention on position of &: in Test& it is on right, but in &b it is on left. Also important is that Test is a type, but b is an object/variable.
  • There are only so many keys on a keyboard, and the letters and digits already have other uses. On an unrelated note, there's no such thing as a "reference operator". operator in C++ has a specific meaning, it applies to expressions not types.
  • @VolAnd While a nice observation, this isn't a good rule to follow as this is completely arbitrary. My favorite example is f(int *&a) or f(int*& a)`. What is this? One might never know.
  • @henrikgiesel This is definitely f(int*& a), i.e. pointer that is passed by reference to function f(). It is a reasonable alternative to using pointer-to-pointer (i.e. f(int** a)) for functions that allocate memory in body (or change address for any other reason)
  • Thanks for your answer really helpful@Fomalhaunt...