tags:

views:

66

answers:

3

here is 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 dont understand wta does sortcmp()? and does skip function return non null terminated part from char *p or what? please explain

A: 

sortcmp takes double pointers (char*) as arguments, wordncmp does not. So sortcmp is a way of calling wordncmp with double pointers (char*). Its like a overload function.

skip does what you wrote:

skip function return non null terminated part from char *p

Martin Ingvar Kofoed Jensen
A: 

I guess sortcmp() returns 0 if the in parameters p and q are equal, or a positive value if p is less than q or a negative value if p is greater than q. That is a useful functions when implementing a sort algorithm.

Johan
+1  A: 

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 first k words, where k is 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 using qsort().
  • skip() skips over n words 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.

Mike Seymour
+1: Looks good to me.
Binary Worrier