General Code for Swapping Both Int and FLOAT

/*just declare i and j as float and then assign vales to them , no other modifications required to modify the code to Swap FLoat values */
#include<stdio.h>
#include<stdlib.h>
void swap(void *vp1,void *vp2,int size);
int main()
{
int i=31,j=32;
swap(&i,&j,sizeof(int));
printf(“%d %d “, i , j);
return 0;
}
void swap(void *vp1,void *vp2,int size)
{
char buffer[size];
memcpy(buffer,vp1,size);
memcpy(vp1,vp2,size);
memcpy(vp2,buffer,size);
}

Advertisements

2 thoughts on “General Code for Swapping Both Int and FLOAT

  1. what if we want to swap two values of different datatype??? the question that i am asking is in receiving the size in the function swap()… which size has to be received? and how will it alter the code if the other size if passed?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s