C job interview - casting and comparing C job interview - casting and comparing c c

C job interview - casting and comparing


If he is really dissatisfied with this approach (which is essentially a brain fart, since you aren't comparing megabytes or gigabytes of data, so one shan't really be worrying about "efficiency" and "speed" in this case), just tell him that you trust the quality and speed of the standard library:

int isEqual(MAC* addr1, MAC* addr2){    return memcmp(&addr1->data, &addr2->data, sizeof(addr1->data)) == 0;}


If your interviewer demands that you produce undefined behavior, I would probably look for a job elsewhere.

The correct initial approach would be to store the MAC address in something like a uint64_t, at least in-memory. Then comparisons would be trivial, and implementable efficiently.


Cowboy time:

typedef struct macA {   char data[6];} MAC;typedef struct sometimes_works {   long some;   short more;} cast_ittypedef union cowboy{    MAC real;    cast_it hack;} cowboy_cast;int isEqual(MAC* addr1, MAC* addr2){     assert(sizeof(MAC) == sizeof(cowboy_cast));  // Can't be bigger     assert(sizeof(MAC) == sizeof(cast_it));      // Can't be smaller     if ( ( ((cowboy_cast *)addr1)->hack.some == ((cowboy_cast *)addr2)->hack.some )       && ( ((cowboy_cast *)addr1)->hack.more == ((cowboy_cast *)addr2)->hack.more ) )             return (0 == 0);     return (0 == 42);}