1#include <stdio.h>2 structpoint{3 intx;4 Doubley;5Point (intx =0,Doubley =0): X (x), Y (y) {}6 voidprint () {7printf"(%d,%LF)", x, y);8 }9 };Ten voidSwap (Char*a,Char*B, size_t width) { One Chartmp; A while(width--){ -TMP = *A; -*a++ = *b; the*b++ =tmp; - } - } - //width is the byte size of the exchange of two elements, with sizeof to find out + voidMyswap (void*x,void*y, size_t width) { -Swap ((Char*) x, (Char*) y, width); + } A intMain () { at intINTA =1, INTB =2; -printf"before exchange:inta=%d\tintb=%d\n", Inta, INTB); - //exchanging values for shaping inta and INTB -Myswap (&inta, &INTB,sizeof(int)); -printf"After exchagge:inta=%d\tintb=%d\n", Inta, INTB); - in CharChara ='a', Charb ='A'; -printf"before exchange:chara=%c\tcharb=%c\n", Chara, Charb); to //Exchange character Chara and character Charb values +Myswap (&chara, &charb,sizeof(Char)); -printf"After exchagge:chara=%c\tcharb=%c\n", Chara, Charb); the *Point Pointa (1,1.1), POINTB (3,3.3); $printf"before Exchange:pointa="); Pointa.print (); Panax Notoginsengprintf"\tpointb="); Pointb.print (); printf"\ n"); - //exchanging values for struct Pointa and POINTB theMyswap (&pointa, &POINTB,sizeof(point)); +printf"before Exchange:pointa="); Pointa.print (); Aprintf"\tpointb="); Pointb.print (); printf"\ n"); the return 0; +}
Program Run Results
Write yourself a swap function to exchange any two values of the same element to determine the size of the null pointer using byte (i)