FoodForFutureGeeks

Sunday 15 April 2012

Introduction to pointers

What is a pointer?

A pointer is a special type of variable that is used to hold the address of another variable or a pointer,pointer can be used to manipulate the value stored at that address.
Often it may be required to manipulate the value stored at a particular address, this is not possible by using normal variables, so we need pointers.(Read article on Pass by value&Pass by reference in general programming.)

Declaring a Pointer: 

Pointer variables are declared by prefixing variable name with * symbol.
ex: int *ipointer;
float *fpointer; etc


Assigning Value to a pointer/pointing it to a variable:

a pointer can be assigned the address of a variable as follows:
int *ipointer;
int k=15;
ipointer=&k;
the & operator is used to obtain the address of variable k.

Dereferencing a pointer/obtaining the value pointed by pointer:

this can be done by prefixing the pointer with *.
ex:
float *fpointer;
float  f=10.00;
fpointer=&f;
//use * to obtain the value pointed by pointer
printf("value pointed by fpointer:%f",*fpointer);

Important Note:

 * is used in two places when dealing with pointers
1. int* ip ; // here it means u are declaring a pointer to an integer.
2. int k=*ip; or printf(“%d”,*ip);  here it means dereferencing or fetching the value stored at the address pointed by pointer.

Sample Program:

#include<stdio.h>
void main()
{
//declaring a pointer
int *pointer;
int k=10;
//storing the address of variable k in a pointer
pointer=&k;
//the symbol & gives the address of variable k
printf("Getting the value From pointer\n");
//here * is used to dereference or obtain the value pointed by pointer
printf("K:%d",*pointer);
}

Two dimensional pointers:

two dimensional pointers are pointers to pointers or they can be considered as an array of pointers

ex1:pointer to a pointer 

char* str="hi im learning pointers";
char** strp=&str;
printf("%s",*strp);

here strp acts as a pointer to str which is pointing to the starting address of string "hi im learning pointers"

ex2 (complicated only for C++ ):

This concept is very useful when an array has to be populated using pass by reference
#include<iostream>
#include<conio.h>
void populatearray(int** mainarray,int* count)
{
    int a[]={1,2,3,4,5};
    //create a single dimension array for storing the values
    int* myarray=new int[5];
    //assign the values to be stored to the array
    for(int i=0;i<5;i++)
    {
        myarray[i]=a[i];
    }
    //store the count in the adress pointed by formal parameter
    *count=5;
    //store the array in the adress pointed by formal parameter
    *mainarray=myarray;
}
void main()
{   //the main array where values have to be stored
    int* arraymain=0;
    //count of elements
    int maincount=0;
    //pass the adess of pointer to array, adress of counter
    populatearray(&arraymain,&maincount);
    //check whether pass by reference has worked
    printf("The array Elements:\n");
    for(int i=0;i<maincount;i++)
    {
        printf("\n%d",arraymain[i]);
    }
    getch();
}

No comments:

Post a Comment