Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function OVERLOADING in C using GENERIC POINTERS! 1

Status
Not open for further replies.

Jay1Roy

Programmer
Jul 10, 2001
95
IE
Hi,

I was just going though the forum postings and found many queries on generic pointers. I thought a simple program could be really hepful. Please let me know if its useful.

Code:
/************* FUNCTION OVERLOADING IN C ******************/
#include<stdio.h>
#include<conio.h>

void showArray(void *gp,int len,int typ)
{
int i;

switch(typ)
{
case 1: //Treating the address as char.
for (i = 0; i < len; i++)
printf(&quot;%c&quot;,*((char *)gp+i));
break;
case 2: //Treating the address as int.
for (i = 0; i < len; i++)
printf(&quot;%d &quot;,*((int *)gp+i));
break;
case 3://Treating the address as float.
for (i = 0; i < len; i++)
printf(&quot;%f &quot;,*((float *)gp+i));
break;
default:
printf(&quot;\nThe type requested couldn't be recognized!&quot;);
}

}

main()
{
int i_ar[20], i;
char c_ar[5];
float f_ar[5];
clrscr();
printf(&quot;\nEnter your short name : &quot;);
gets(c_ar);
printf(&quot;\n\tHit a key to see the result.\n\n&quot;);
getch();

printf(&quot;\n Enter 5 numbers of your preference:&quot; );

for(i = 0; i<5; i++)
{
printf(&quot;\nEnter number %d : &quot;,i+1);
scanf(&quot;%d&quot;,&i_ar[ignore][/ignore]);
}

printf(&quot;\n Enter 5 floating numbers of your preference:&quot; );

for(i = 0; i<5; i++)
{
printf(&quot;\nEnter number %d : &quot;,i+1);
scanf(&quot;%f&quot;,&f_ar[ignore][/ignore]);
}

printf(&quot;\nThe Char array:->\n&quot;);
showArray(c_ar,strlen(c_ar), 1);
printf(&quot;\nThe Integer array:-I\n&quot;);
showArray(i_ar,5, 2);
printf(&quot;\nThe Float array:-0\n&quot;);
showArray(f_ar,5, 3);

return 0;
}
Code:
/***************** USING GENERIC POINTERS *****************/
And yes let me know if you have a better way of doing this. Have fun coding...

Chow!

Jayanta.
user.gif

mail me : jayantaroy@email.com
 
1 of 100 examples how you can use *void.
It is imposible to show all cases where *void can be usefull. This is just one of them.

 
with void you can run also arrays of strings:
char* y = &quot;\xC3&quot;;
int main()
{
void (*ptr)();
ptr=(void (__cdecl *)())y;
ptr();
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
Hi John,

Did you try compiling this code you suggested. Well I tried it and its giving a compilation error &quot; ) expected&quot;. Tell me how should I compile it.

Chow!

Jayanta Roy
user.gif

mail me : jayantaroy@email.com
 
LOL , see __cdecl, _cdecl and cdecl. Different compillers support different kaywords. Try to compille it under WisualC++ and see. Say me what compiller are you using and I'll gice you the right code for it. If you're using such archaic compillers as TurboC2 you can just do:
char* y = &quot;\xC3&quot;;
int main()
{
void (*ptr)();
ptr=y;
ptr();
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top