pointer

Here we will discuss following,

 

Pointer

Pointer is a derived data type. In C++ or any other programming language, in the program we first declare the variable to perform any task which contains the input or information store given by the end-user. variable stores these values ​​in memory.

Now as in memory each information is stored as a byte and these bytes have their own address, so that means which value stored in the memory they have their own address.

In C++ we can access address of these values ​​using a pointer variable.

pointer

So definition is,

A pointer is a type of variable, which stores or holds the address of values/variable. By using pointer variable in C++ we can access the address of any variable and manipulate them.

 


Declaration of a pointer variable

The pointer-variable is like a normal variable, but when we add asterisk (*) to it, it becomes a pointer variable. as follows,

SYNTEX:

data-type *variable_name;

Here, data can be any data-type like- int, char, float and void .

asterisk sign can declare anywhere between data type and variable-name, such as

Data-type* variable-name;

Remember that, there should be the same data type as the data type variable you want to store the address. That is, we cannot store the address of a variable of type char or float with variables of type int. i.e-

int x; float *y;
y = &x; // wrong
int x,*y;
y = &x; // right


To access the address of a variable, we use 2 types of operator in pointer.

  • value of operator (*)
  • address of operator (&)

 


value of operator (*)

It is used to print store value in address. It is also called de-reference operator.

int *ptr;
int x = 6;
cout<<*ptr1; // 6 

 


address of operator (&)

It is used to print the address of value. It is also called reference operator like-

int *ptr, x = 6;
ptr = &x;
cout<<ptr; // address will be print

without using the de-reference operator (*), we can print the address of a variable, i.e. the address of a variable can also be printed using only the & operator. like,

cout<<&x; // x variable address will be print

using the operator, we can also access the address of the pointer variable itself.like,

cout<<&ptr; // pointer-variable address will be print

From this you can say that we use the pointer variable to store only the address of the variable which other ordinary variable cannot store.

Here is complete program, where we user both method,

Example:-
In the program, we have access to the address of variable x in two ways. First, using the pointer-variable, the second one is accessing the variable address using the & operator, then assigning or storing it in a pointer-variable and secondly we have printed the variable directly using the reference operator.

 

access a variable address using pointer-variable

#include<iostream.h>
#include<conio.h>
void main()
{
   int x = 6,*ptr;
   ptr = &x;

   clrscr();
   cout<<"value store the address: "<<*ptr<<endl;
   cout<<"Addres of this variable: "<<ptr<<endl;
   cout<<"\nanother way using &x : "<<&x<<endl;
   cout<<"Addres of pointer varible: "<<&ptr<<endl;
getch();
}
OUTPUT:
value store the address: 6
Addres of this variable: 0x8f29fff2

another way using &x : 0x8f29fff2
Addres of pointer varible: 0x8f29fff0

pointer is used for accessing the address of a variable, as well as for dynamic memory allocation.

more about pointer


previous-union in C++

next-Memory Management and Types