Pointers

0 cheriroark7212 November 13, 2020

A pointer is a variable thta conatins a memory address of data or another variable. Normally, a pointer variable is declated to some type, like any other variables, so thta it will work only with data of given type. 

The syntax of pointer is

data_type *pointer;

Pointer Declaration

int *x;

In the first statement, x is an integer pointer and it tells to the compiler that it holds the address of any int variable.

Adress Operator

The operator & is known as aqddress operator. Thus &a denotes the address of variable a.

Lets take an example to make clear concepts on pointer.

#include <iostream>
using namespace std;
int main(){
   //Pointer declaration
   int *p, var=101;
 
   //Assignment
   p = &var;

   cout<<"Address of var: "<<&var<<endl;
   cout<<"Address of var: "<<p<<endl;
   cout<<"Address of p: "<<&p<<endl;
   cout<<"Value of var: "<<*p;
   return 0;
}

The output of the above program is:

Address of var: 0x7fff5dfffc0c
Address of var: 0x7fff5dfffc0c
Address of p: 0x7fff5dfffc10
Value of var: 101

In the next tutorials, We will learn how to use pointers and array.