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)

Source: Internet
Author: User

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, &AMP;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, &AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.