jeudi 26 août 2021

C function that compares two blocks of memory [closed]

In an interview, I was asked this question and I couldn't get through it. after the interview, I managed to crack the code but could not review it from an expert. I would be glad if someone can review this code. or if there are any mistakes, try to correct them.

Thank you in advance

Question:

/* 
 * Write a C function that compares two blocks of memory starting at location s1 and s2 with size numBytes.
 * Return 1 if blocks are exactly the same and 0 if they are in anyway different. 
 * 
 * Use the following function prototype:
 * 
 * int blockCompare(char *s1, char *s2, int numBytes)
 *
 */

Answer:

int blockCompare(char *s1, char *s2, int numBytes) {

   int a = sizeof(*s1);
   int b = sizeof(*s2);

   numBytes = a - b;

   if (numBytes = 0) {
      return 1;
   } else
      return 0;

Aucun commentaire:

Enregistrer un commentaire