. Document the function mystery. The documentation should explain what each line
ID: 3627273 • Letter: #
Question
. Document the function mystery. The documentation should explain what each linedoes and also give a general overview of what mystery does, that is, how mystery
computes the return value from the two arguments.
#include <stdio.h>
int mystery(int n1, int n2) {
unsigned char* ptr1 = (unsigned char*) &n1;
unsigned char* ptr2 = (unsigned char*) &n2;
int ret_val;
unsigned char* ptr3 = (unsigned char*) &ret_val;
ptr2 += sizeof(int) - 1;
ptr3 += sizeof(int) - 1;
int i;
for (i = 0; i < sizeof(int); i++) {
*ptr3 = *ptr2;
ptr3--;
ptr2--;
}
ptr3++;
*ptr3 = *ptr1;
return ret_val;
}
Explanation / Answer
// no mystery in this..
// you are just returning first parameter
int mystery(int n1, int n2) {
unsigned char* ptr1 = (unsigned char*) &n1;
// get address of n1 ptr1 points n1
unsigned char* ptr2 = (unsigned char*) &n2;
// get address of n2 ptr2 points n2
int ret_val;
unsigned char* ptr3 = (unsigned char*) &ret_val;
// get address of n1
ptr2 += sizeof(int) - 1;
ptr3 += sizeof(int) - 1;
int i;
// here some operation is going it doesnot matter what it is because
for (i = 0; i < sizeof(int); i++) {
*ptr3 = *ptr2;
ptr3--;
ptr2--;
}
ptr3++;
*ptr3 = *ptr1;
// here u are assiginig value of ptr3 to ptr1 which is n1
// again ptr3 points to ret_val
// thus ret_val = n1
// same as return n1
return ret_val;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.