/* */ Click to Join Live Class with Shankar sir Call 9798158723

Pointer in C++ ?


A variable that contains the address of another variable or address of a memory location is called a pointer. A pointer is also called a pointer variable. A pointer variable can store address of other variables.

Pointer declaration and definition

In C++ language we know that all the variables should be declared before they are used, pointer variables should be declared before they are used.

Here, we see how to declare pointer variable.
Syntax:-
data * identifier;
  • Declare a data variable Ex:- int a;
  • Declare a pointer variable Ex:- int*p;
  • Initialize a pointer variable Ex:- p= & a;
  • Access data using pointer variable Ex:- printf("%d",*p);

Q.)W.A.P to initialize pointer to variable.
// C++ program to illustrate Pointers

#include 
using namespace std;
void geeks()
{
	int var = 20;

	// declare pointer variable
	int* ptr;

	// note that data type of ptr and var must be same
	ptr = & var;

	// assign the address of a variable to a pointer
	cout << "Value at ptr = " << ptr << "\n";
	cout << "Value at var = " << var << "\n";
	cout << "Value at *ptr = " << *ptr << "\n";
}
// Driver program
int main()
{
geeks();
return 0;
}