Pointer Variables in c++ language
pointer variable is worked on reference in a variable. it’s used to address of a variable.
int x;
int &r = x;
This declaration creates a variable r that refers to the same memory location as the variable x. We say that r aliases x. Unlike a pointer variable, we may treat r as if it were an int variable with * is necessary. demonstrates how reference variables can alias other variables.
Reference variables are similar to pointer variables, but there are some important differences. Reference syntax is simpler than pointer syntax because it is not necessary to dereference a reference variable in order to assign the memory location to which it refers. If num is an int, ptr is a pointer to an int, and ref is a reference to an int, consider the following statements:
num = ref; // Assign num from ref, no need to deference
num = *ptr; // Assign num from ptr, must dereference with *
A reference variable must be initialized with an actual variable when it is declared. A pointer variable may be declared without an initial value and assigned later. Consider the following statements:
int *p; // Legal, we will assign p later
int& r; // Illegal, we must initialize r when declaring it
Also, unlike with pointers, it is illegal to attempt to assign nullptr to a reference variable.
There is no way to bind a reference variable to a different variable during its lifetime. Consider the following code fragemnt.
follow this