Search code examples
c++pointersvariable-declaration

Difference between declaring variables like &ref=a and ref=&a in C++


I'm little confused about two types of i came across like if int a=0, what does it "&ref=a" refers to and what "ref=&a" this thing refers to?

do these serves the same purpose of holding address of variable?


Solution

  • &ref=a : It is the reference variable means it's is the another name for existing variable. It can be used like normal variables.Memory management of references left up to compiler

    *ref=&a : It is the pointer variable it holds the address of another variable. Pointers are used to store and manage the addresses of dynamically allocated blocks of memory.

    And the my answer is YES, Both should serves the same purpose in holding variable because in most compilers the reference can thought as constant pointer itself.