CS Electrical And Electronics
@cselectricalandelectronics

What is pointer? With example.

All QuestionsCategory: Cpp LanguageWhat is pointer? With example.
CS Electrical And Electronics Staff asked 4 years ago

I need short information

1 Answers
CS Electrical And Electronics Staff answered 4 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