English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The reference variable is an alias for an existing variable. It cannot be changed to reference another variable and should be initialized at the declaration. It cannot be NULL. The operator “&” is used to declare a reference variable.
The syntax of the reference variable is as follows.
datatype variable_name; // Variable declaration datatype& refer_var = variable_name; // reference variable
Here,
datatype-The data type of the variable, for example, int, char, float, etc.
variable_name-This is the variable name given by the user.
Refer_var-The name of the reference variable.
The following is an example of a reference variable.
#include <iostream> using namespace std; int main() { int a = 8; int& b = a; cout << "The variable a: " << a; cout << "\nThe reference variable r: " << b; return 0; }
Output result
The variable a: 8 The reference variable r: 8
Basically, a pointer is a variable that stores the address of another variable. When memory is allocated for a variable, the pointer points to the address of the variable.
The syntax of pointers is as follows.
datatype *variable_name;
Here,
datatype-The data type of the variable, for example, int, char, float, etc.
gvariable_name-This is the name of the variable given by the user.
The following is an example of a pointer.
#include <stdio.h> int main () { int a = 8; int *ptr; ptr = &a; printf("Value of variable: %d\n", a); printf("Address of variable: %d\n", ptr); printf("Value pointer variable: %d\n",*ptr); return 0; }
Output result
Value of variable: 8 Address of variable: -201313340 Value pointer variable: 8