Search This Blog

Tuesday, 23 October 2012

Write a program using pointers

#include<iostream.h>
void main()
{
int i,*iptr;
iptr=&i;
i=20;
cout<<"i value is"<<i<<endl;
*iptr=*iptr+30;
cout<<"i value is"<<i<<endl;
}

Output:
i value is 20
i value is 50 

Pointers
Pointer data type is one of the strength of c++ language. The pointer is a powerful techniques to access the data by indirect references as it hold the address of that variable. A pointer is a variable which holds the memory address of another varaiable.

Advantages of Pointer
  • It allows to pass varables, arrays, functions, strings and structures as function arguments.
  • It allows to establish links between data elements or objects for some complex data structures such as linked lists, stacks, queues, binary trees. 

Declaration
The general format of pointer declaration is,
data_type *pointer_variable;

for example 
int a;
int *ptr;
a=10;
ptr=&a;
where ptr is a pointer variable which holds the address of an integer variable a
100 -->address of a                200-->address of ptr
    -->value of a                     -->value of ptr
       a                                 ptr

consider the following statement
  a      -->    refers to 10
  &a     -->    refers to 100
  *(&a)  -->    refers to 10
  ptr    -->    refers to 100
  &ptr   -->    refers to 200
  *ptr   -->    refers to 10
 


1 comment:

  1. please help me to create a switch-case c++ program using pointers:

    a)ascending
    b)descending
    c)highest value
    d)sum&average
    e)difference from highest-lowest
    f)searching inputted value if found print its location
    g)exit

    5 input value of the user..pls help me tnx so much

    ReplyDelete