What is pointer? With example.

All QuestionsCategory: Cpp LanguageWhat is pointer? With example.
Chetan Shidling Staff asked 5 years ago

I need short information

1 Answers
Chetan Shidling Staff answered 5 years ago

Pointers

A pointer is a derived data type that refers to another data variable by storing the variables memory address rather than data.

Declaring and initializing pointers:

data-type *pointer-variable;
Eg: int *ptr , a;
ptr = &a;

Example of using pointer:

#include <iostream>
using namespace std;
int main()
{
int a, *ptr1, **ptr2;
ptr1 = &a;
ptr2 = &ptr1;
cout<<“The address of a : ” <<ptr1 <<“\n”;
cout<<“The address of ptr1 : ” <<ptr2 <<“\n”;
cout<<“After incrementing the address values : \n”;
ptr1+=2;
cout<<“The address of a :”<<ptr1 <<“\n”;
ptr2+=2;
cout<<“The address of a :”<<ptr2 <<“\n”;
return 0;
}

Output:

The address of a : 0x69fed8
The address of ptr1 : 0x69fed4
After incrementing the address values :
The address of a :0x69fee0
The address of a :0x69fedc