Hi was wondering if I am comparing 2 char arrays of the same size, can i use memcmp?
Or is there a better way of comparing?
Hi was wondering if I am comparing 2 char arrays of the same size, can i use memcmp?
Or is there a better way of comparing?
If that's the type of comparison you need, it's fine. Did you try it? Is that the type of comparison you need?
lol. but if i want to use memcmp between an uint8_t array of size 2 and a uint16_t variable, does it still work? since its comparing the memory at the address right?
Code:union moarcheating { uint8_t a[2]; uint16_t b; } a, b; a.a[0] = 0xAA; a.a[1] = 0xBB; b.b = 0xAABB; /* assuming your endian is correct */ if( b.b == a.b ) printf( "omg hax\n" );
Quzah.
Hope is the first step on the road to disappointment.
But if your uint16 contains 0x1234 and your uint8 array contains [ 0x12, 0x34 ] then it is down to the endian of your machine as to whether they compare equal or not (using memcmp)
You're better off with
if ( (var & 0xff) == array[0] && ((var>>8)&0xff) == array[1] )
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.