Write a C program to swap content of memory blocks of size 32 bytes

It’s similar to the way you can use a temporary variable to swap two integers:

int t = a;   // temporarily save a
a = b;       // overwrite a with b
b = t;       // overwrite b with saved a

In this case, you can create a temporary buffer of size 32 bytes with the following declaration:

char tempbuff[32];

and then use that to swap the contents of the two other 32-byte buffers, keeping in mind that memcpy can copy arbitrary chunks of memory, as with:

memcpy (tempbuff, firstbuff, 32); // copy firstbuff to tempbuff

That, combined with the earlier int t = a; a = b; b = t; method, should be more than enough to get you writing a complete program to do the job you need.

Leave a Comment