Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Since C and C++ have pointer variables, another approach is possible. Write a C

ID: 3776507 • Letter: S

Question

Since C and C++ have pointer variables, another approach is possible. Write a C program with a function that changes the byte order of an integer by treating it as an array of characters. The function should be defined as the following:

void swap ( char* data ) { ... }

It should be called like the following:

int k = 0x0a0b0c0d; swap( (char*) &k );

After swap, the byte order of k should become 0x0d0c0b0a.

Deliverables: You are required to test your program using the following inputs: 0x01020304, 0x010203ff, 0xff020304, 0xfe020304, and 0x80aacc84.

Explanation / Answer

void swap ( char* data )

{

char* d = data + 2;

  int s = sizeof(d);

int i = 1;

while(i < s)

{

char t = d[i];

d[i] = d[s-i];

d[i] = t;

i = i+2;

}

}