Here are functions on strings from programming pearls.
int wordncmp(char *p, char* q)
{ int n = k;
for ( ; *p == *q; p++, q++)
if (*p == 0 && --n == 0)
return 0;
return *p - *q;
}
int sortcmp(char **p, char **q)
{ return wordncmp(*p, *q);
}
char *skip(char *p, int n)
{ for ( ; n > 0; p++)
if (*p == 0)
n--;
return p;
}
I don’t understand what does sortcmp() do? And does skip function return non null terminated part from char *p or what?
Please explain.
This is complete guesswork, since I don’t have a copy of the book, but it looks like these functions are for working with an unconventional string format consisting of a sequence of “words” separated by null characters.
wordncmp()compares the firstkwords, wherekis presumably a global variable to be set before calling the function.sortcmp()takes pointers to string pointers, and is presumably intended as the comparator when sorting an array of string pointers usingqsort().skip()skips overnwords in a string.In C++, you’d be better off using the standard String and Algorithms libraries to do this sort of thing; there’s rarely a good reason to mess around with pointers and unconventional string representations.