/*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
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?
The code cannot be used to swap data type with different size….Since both INT and FLOAT are of same size the code can swap both ….