Given datatype of two variable as an integer value and address of two variables as a void pointer to the function common_swap(). Do swap the values pointed by the two pointer variables. The integer value from 1 to 6 means the datatype. 1. char(%c) 2. short int(%hd) 3.long int(%ld) 4. long long int(%lld) 5. float(%f) 6. double(%lf).
Question
Given datatype of two variable as an integer value and address of two variables as a void pointer to the function common_swap(). Do swap the values pointed by the two pointer variables. The integer value from 1 to 6 means the datatype. 1. char(%c) 2. short int(%hd) 3.long int(%ld) 4. long long int(%lld) 5. float(%f) 6. double(%lf).
Solution
Here is a C function that swaps the values pointed by two void pointers. The function uses a temporary buffer for swapping and the size of the buffer is determined by the datatype:
#include <stdio.h>
#include <string.h>
void common_swap(void *vp1, void *vp2, int datatype) {
char buffer[16];
switch(datatype) {
case 1: // char
memcpy(buffer, vp1, sizeof(char));
memcpy(vp1, vp2, sizeof(char));
memcpy(vp2, buffer, sizeof(char));
break;
case 2: // short int
memcpy(buffer, vp1, sizeof(short int));
memcpy(vp1, vp2, sizeof(short int));
memcpy(vp2, buffer, sizeof(short int));
break;
case 3: // long int
memcpy(buffer, vp1, sizeof(long int));
memcpy(vp1, vp2, sizeof(long int));
memcpy(vp2, buffer, sizeof(long int));
break;
case 4: // long long int
memcpy(buffer, vp1, sizeof(long long int));
memcpy(vp1, vp2, sizeof(long long int));
memcpy(vp2, buffer, sizeof(long long int));
break;
case 5: // float
memcpy(buffer, vp1, sizeof(float));
memcpy(vp1, vp2, sizeof(float));
memcpy(vp2, buffer, sizeof(float));
break;
case 6: // double
memcpy(buffer, vp1, sizeof(double));
memcpy(vp1, vp2, sizeof(double));
memcpy(vp2, buffer, sizeof(double));
break;
}
}
This function uses the memcpy function to copy the contents of the memory area pointed to by vp1 to the temporary buffer, then copies the contents of the memory area pointed to by vp2 to vp1, and finally copies the contents of the temporary buffer to vp2. The size of the memory areas to be copied is determined by the datatype.
Similar Questions
12345678910111213141516#include <stdio.h>void swap(int *x, int *y) { int temp = *x; *x = *y; *y = temp; printf("%d %d", *x, *y);}int main() { int a = 120; int b = 121; printf("%d %d\n", a, b); swap(&a, &b); return 0;}
Using pointers write a program to swap two numbers using a user definedfunction(Call by reference)
Consider the following C functionvoid swap (int a, int b){ int temp; temp = a; a = b; b = temp;}.In order to exchange the values of two variables x and y.
Arun is working on a program to swap the values of two variables, a and b, without using a temporary variable. Write a program to help Arun accomplish this task.Input format :The first line of input is an integer value a.The second line of input is an integer value b.Output format :The output prints the values of a and b after swapping. Refer to the sample output for the formatting specifications.
What is the output of this C++ code? #include <iostream>using namespace std;void swap(int a,int b){ int temp = a; a=b; b=temp;}int main(){ int a = 1, b = 2; swap(a,b); cout<<a<<" "<<b; return 0;} 1 22 21 12 1
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.