Bethell)says...
<< stuff deleted >>
My comments are within the source......
Quote:
>#include <search.h>
>#include <string.h>
>#include <stdio.h>
>int compare (const void *arg1, const void *arg2);
>void main( unsigned int argc, char **argv)
>{
> char **result;
> char *key = "hello";
> result = (char **)_lfind(&key, argv, &argc, sizeof(char *), compare);
Look at the prorotype of _lsearch
void *_lsearch( const void *key, const void *base, unsigned int *num, unsigned int
width, int ( __cdecl *compare )( const void *elem1, const void *
elem2 ) );
Whar the call is doing is comaring the pointer to strings, but since the strings in
the argv are of different lengths, it is passing the pointer to the pointer to the
0th character of the string. This explains `&key`, and `argv`
`compare` in the prototype is declared as a pointer to the function taking `const
void *elem1 and `const void *elel2` as the two arguments and returning an integer.
The name of the function is the pointer to the function thus only `compare` is
passed because it is also the name of the function that will decide which string
pointer is to be considered large.
Quote:
if result is non-NULL, "hello" was found.
If the result is
Quote:
> printf ("%s found!\n", *result);
> else
> printf("hello not found!\n");
>}
>int compare (const void *arg1, const void *arg2)
>{
> return (_stricmp(*(char**)arg1, *(char**)arg2));
>}
This function compares the strings pointed by the pointer pointed by arg1 and arg2
using the _stricmp (case insensitive compare).
<< some art deleted >>>
Quote:
> Still with me? I really hope so!
Yes
Quote:
> Questions! questions!! questions!!!
> - The prototype for compare declares a couple of parameters:
> (const void *arg1, const void *arg2);
The `const` means that the arg1 is a pointer to a constant string i.e. the function
may not modify the strings.
Quote:
> Come again!!!
> What are these parameters saying/doing?
> - Why would argv be declared as a pointer to a pointer, and
> how is it being used? result too???
this is necessary in this little example because we are actually passing pointers to
the string pointers to do the work for us. You will not have to pass ptr to ptr in
case you are comparing anything of fixed width, say long, or int
Quote:
> - the call to _lfind() is about the most confusing thing I have
> come accross so far with C. Could someone please take the time
> to explain this call to me. Specifically, why the addresses
> of key and argc are passed, why compare is part of the call, but
> especially, why there are no '()' or parameters with it.
I discussed some of this up there. _lfind() does a linear search. Let me quote from
the MSVC manual :]
key Object to search for
base Pointer to base of search data
num Number of elements
width Width of elements
compare Pointer to comparison routine
elem1 Pointer to the key for the search
elem2 Pointer to the array element to be compared with the key
The _lsearch function performs a linear search for the value key in an array of num
elements, each of width bytes in size. (Unlike bsearch, _lsearch does not require
the array to be sorted.) The base argument is a pointer to the base of the array to
be searched.
If key is not found, _lsearch adds it to the end of the array.
The compare argument is a pointer to a user-supplied routine that compares two array
elements and returns a value specifying their relationship. The _lsearch function
calls the compare routine one or more times during the search, passing pointers to
two array elements on each call. This routine must compare the elements, then return
one of the following values:
Value Meaning
Nonzero Elements are different
0 Elements are identical
Return Value
If the key is found, _lsearch returns a pointer to the element of the array at base
that matches key. If the key is not found, _lsearch returns a pointer to the newly
added item at the end of the array.
Use _lsearch for compatibility with ANSI naming conventions of non-ANSI functions.
Use lsearch and link with OLDNAMES.LIB for UNIX compatibility.
Quote:
> - Why do the two functions lfind and stricmp have a '_' prefixed to
> them? Personally, I think they would look much prettier without
> it !!!! :-)
_stricmp() is a MSVC Small DATA model specific function which is (possibly)
optimized for 16 bit address. Also it does an case insensitive compare. It is
declared with the leading _.
Quote:
> - And finally, what's the story with those casts in the stricmp call?
looka at the prototype of _stricmp() and you will realize that the function compare
receives pointers to pointers to void. you have pass pointers to char to _stricmp.
so.
Quote:
>Hopefully, someone can help me (email would be nice!!). In the meantime,
>I'll study them manuals and see what I can see....
>Cheers folks!!
>___ Blue Wave/QWK v2.12
hope that helps....
mag
--
/*
To understand recursion one must first understand recursion
*/