#define ALLOW_INSTRUCTOR_CODE /*************************************************************************** *************************************************************************** ****** BEFORE BEGINNING OR TURNING IN THIS EXERCISE IT IS EXTREMELY ****** ****** IMPORTANT THAT YOU READ, FULLY UNDERSTAND, AND ADHERE TO THE ****** ****** REQUIREMENTS STATED IN THE DOCUMENT TITLED, "ASSIGNMENT ****** ****** SUBMISSION REQUIREMENTS", DOWNLOADABLE FROM THE COURSE WEB ****** ****** SITE. ****** *************************************************************************** **************************************************************************/ /*************************************************************************** ************* C/C++ Programming 2: Assignment 5 - Exercise 1 ************* ********* BEGIN EXERCISE REQUIREMENT - THIS MUST BE A "C" PROGRAM ********* *************************************************************************** (2 points) Write function SwapObjects: SwapObjects syntax: void SwapObjects(void *pa, void *pb, size_t size); Parameters: pa - a pointer to one of the objects to be swapped pb - a pointer to the other object to be swapped size - the number of bytes in either object Synopsis: Swaps the objects in pa and pb regardless of size or data type as long as both objects are the same size. Return: void * Do not copy byte-at-a-time. * If SwapObjects dynamically allocates memory it must free it before returning. *************************************************************************** ************************ END EXERCISE REQUIREMENT ************************* **************************************************************************/ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING ABOVE IT. **************************************************************************/ /* * TODO: * REPLACE THIS COMMENT WITH YOUR TITLE BLOCK. */ /* * TODO: * REPLACE THIS COMMENT WITH YOUR C CODE, PLUS ANYTHING NECESSARY TO * SUPPORT MY TEST CODE BELOW. DO NOT INCLUDE ANY UNNEEDED HEADER FILES! */ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING BELOW IT. **************************************************************************/ /* ***** Change nothing below this comment without instructor permission. **** * Everything that follows was written to help test/verify your code. You * do not need to understand the details of my code to write yours! */ #ifdef ALLOW_INSTRUCTOR_CODE int main(void) { /* Create some test arrays whose elements will get swapped. */ /* Create some test arrays whose elements will get swapped. */ int iArray[] = {0x5555, 0xAAAA}; int iArrayCopy[2]; struct xyz {char c; double d;}; struct xyz sArray[] = {{'0', 1.234}, {'Z', 5.678}}; struct xyz sArrayCopy[2]; /* Copy initialized arrays into uninitialized arrays. */ memcpy((void *)iArrayCopy, (void *)iArray, sizeof iArray); memcpy((void *)&sArrayCopy, (void *)&sArray, sizeof sArray); /* Swap elements in test arrays. */ SwapObjects((void *)&iArray[0], (void *)&iArray[1], sizeof *iArray); SwapObjects((void *)&sArray[0], (void *)&sArray[1], sizeof *sArray); /* Test if swaps worked. */ if (iArray[0] != iArrayCopy[1] || iArray[1] != iArrayCopy[0] || sArray[0].c != sArrayCopy[1].c || sArray[1].c != sArrayCopy[0].c || sArray[0].d != sArrayCopy[1].d || sArray[1].d != sArrayCopy[0].d) fprintf(stderr, "SwapObjects failed\n"); else printf("SwapObjects succeeded\n"); return EXIT_SUCCESS; } #endif