how to do a lexicographic compare between two pointers with unknown byte size,
assuming both array size is equal,
eg implementing the following function to return -1 or 0 or 1 based on the comparecement and converting each byte to ascii
compare(const void *p1, const void *p2, int size)
EDIT: i added another argument with the size of bytes to compare, how can i cast each byte to char and compare between them ?
You’re probably looking for the
strcmp(3)function.Update
If your arrays are not of ASCII strings and may contain the
0x00byte at any position without actually meaning anything, then you’ll need the length as Oli points out, so you can use thememcmp(3)function:Update #2
Okay, now that we’ve established you also want the byte that differs, you’ll need your own function. Something like this (untested):
This will let you find the byte values for whichever bytes differ. Note the
-1return in case of no difference. This limits you to comparing objects whose size fit entirely in assize_t, which is smaller than thesize_ttype — but since you’re comparing two of them, I figure each one gets half the process address space at most anyway.