Pointer Simple Example Program with Reference operator (&) and Dereference operator (*)
Pointer Definition
The pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.
Accessing The Address:
& - Locate the Variable Address
&variable
ponter_variable = &variable;
Declaring Pointer Variables
- * and variable name
- pointer variable points to a variable of type data_type
Syntax:
data_type *pt_name;
Example:
int *p;
float *x;
Initialization Of Pointer Variable:
pointer_vaibale = &variable;
Reference operator ("&")
The reference operator noted by ampersand ("&"), is also a unary operator in c languages that uses for assign address of the variables. It returns the pointer address of the variable. This is called "referencing" operater.
Dereference operator ("*")
The dereference operator or indirection operator, noted by asterisk ("*"), is also a unary operator in c languages that uses for pointer variables. It operates on a pointer variable, and returns l-value equivalent to the value at the pointer address. This is called "dereferencing" the pointer.
1).Normal Variable
int var;
2). Pointer Variable Declaration for Integer Data Type
int *pt;
3). now var == 0
var = 0;
4). & takes the address of var , Here now pt == &var, so *pt == var
pt = &var;
5). Assign Values using dereference operator
*pt = 1;
its equivalent to var = 1, since *pt == var, now *pt == 1 and *pt == var, so var == 1
Pointer Simple Example Program for understand Reference operator (&) and Dereference operator (*)
/*##Pointer Example Program with Reference operator (&) and Dereference operator (*)*/
/*##Simple Pointer Programs,Reference operator,Dereference operator*/
// Header Files
#include <iostream>
#include<conio.h>
using namespace std;
int main() {
//Pointer Variable Declaration for Integer Data Type
int* pt;
int var;
cout << "C++ Pointer Example for Reference operator (&) and Dereference operator (*)\n";
var = 1;
cout << "Address of var :" << &var << "\n";
cout << "Value of var :" << var << "\n\n";
//& takes the address of var , Here now pt == &var, so *pt == var
pt = &var;
cout << "Address of Pointer pt :" << pt << "\n";
cout << "Content of Pointer pt :" << *pt << "\n\n";
var = 2;
cout << "Address of Pointer pt :" << pt << "\n";
cout << "Content of Pointer pt :" << *pt << "\n\n";
//Assign Values using dereference operator
*pt = 3;
cout << "Address of var :" << &var << "\n";
cout << "Value of var :" << var << "\n\n";
getch();
return 0;
}
Sample Output:
C++ Pointer Example for Reference operator (&) and Dereference operator (*)
Address of var :1565276140
Value of var :1
Address of Pointer pt:1565276140
Content of Pointer pt:1
Address of Pointer pt:1565276140
Content of Pointer pt :2
Address of var :1565276140
Value of var :3
(output may vary based on system memory)
C++ Pointer Example Programs
- Simple Pointer Example Program In C++
- Simple Program for Print address of Variable Using Pointer in C++
- Pointer Simple Example Program with Reference operator (&) and Dereference operator (*)
- Simple Example Program for Swap Numbers Using Pointers In C++
- Print size of different types Using Pointer in C++
- Simple Program for Add Two Numbers Using Pointer in C++
- Simple Program for Increment and Decrement Integer Using Pointer in C++
- Simple Program for Increment and Decrement Floating Point Using Pointer in C++
- Simple Program for Find a difference between two Numbers Using Pointer in C++
- Simple Program for Print String Using Pointer in C++
- Simple Program for Count vowels String Using Pointer in C++
- Simple Program for Length of String Using Pointer In C++
- Pointer to Pointer or Double Pointer Example Program In C++
- Simple Program for Pointer and Array Example in C++
- Simple Program for Sum of Integer an array using pointers in C++
- Simple Program for Read, Print and Sum of Integer in an array using pointers in C++
- Simple Example Program for Passing pointers to functions In C++
- Simple Example Program for Area Of Circle Using Pointer In C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming