Krify Articles
  Sign Up | Log In     Krify Home | Videos | Buzz | Free SMS | Matrimony | Answers | Web Hosting | Bulk SMS | Mail | News | Blogs


Music | Shopping | Forums | Freelance | Wall papers | Classifieds | Web director | Greetings | Bookmarks | Search | Games | Adman

Home Recent Articles Popular Articles Article of the day
Category list


Advertisements Ask Questions, Share Knowledge with Krify Answers
 
Posted by:  deepraj majumdar
 Article viewed:  1049  times



How To Make Efficient C Program Part-II

C supports a special kind of pointer known as void pointer.
It s a generic pointer means it can be used for storing
any type of pointer(int,float,char etc).We can reduce complexity
and number of pointer using void pointer.

        We can declare a pointer as void as we declared any
other pointer.E.g-void *ptr;

        Advantage:i)Intialistining any pointer of any type.
                          ii)No type casting is require while intializing.
                          iii)it s may useful when data type is unknown.
                          iv)We can assign a void pointer to another non void pointer.

        Disadvantage:i)No arithmetic operation is not possible in void pointer.


        Use of void pointer-
     

#include <stdio.h>
#include<conio.h>
void call(void *ptr);                  /*function prototype*/

void main()
{
float f=90.88;
int i=80;
void *vptr;
clrscr();

printf(" Before Vpointer(integer)=%d",i);
call(&i);
printf(" After Vpointer(integer)=%d",i);

printf(" Before Vpointer=%.2f",f);
call(&f);
printf(" After Vptr=%.2f",f);

getch();
return;
}

void call(void *ptr)
{
char ch;

printf(" Enter the data type(i=integer/f=float)?");
ch=tolower(getch());

if(ch== f )
{
printf(" Enter the value:");
scanf("%f",ptr);
}
else
{
printf(" Enter the value:");
scanf("%d",ptr);
}
}


In the above example a single function(name call) used for taking any type of data.
Here i only accept Float&Integer value.(But you can use any type of data by modifying the function)

But it has a problem i.e for taking any value I used a option e.g(for integer
you have to press i or f for float)as if i used %d in scanf then it only accept integer value or
if i used %f then it only accept only floating value.

 

Auther:Deepraj Majumdar

Add:kolkata,West Bengal

 e-mail:deep05kol@yahoo.com


Disclaimer: The above article is responsible of the individual who post, krify.com does not hold responsible for any kind of disinformation. If you discover one or more of the krify.com pages direct you to messages that harass, abuse, have obscene, unlawful, defamatory, libellous, hateful, or otherwise objectionable content; or have spam, please inform to krify.com and that will be deleted as soon as possible.

Other interesting Article in the Category Programming
  Write Your First Application in Win32 using Assembly Language
  APPLYING FORMULA IN A TABLE
  The First Cup of Java
  Dynamic-link library
  XML Parsing using PHP (Easy)
  Prevent mistypings to the String class
  Memory Management with PHP
  Take Command with AJAX
  Introduction to C++
  Event Handling and Interfaces in .NET

Write your Comment
Name:
Message:
 Verification code:    

Home Email to Friend
Google Pack

© Copyright - 2007 KRIFY SOFTWARE TECHNOLOGIES (P) LTD