Re: First C Program, Problems getting serial data
Keith Thompson wrote:[color=blue]
> regis <regis@dil.un iv-mrs.fr> writes:[color=green]
>>Rod Pemberton wrote:[/color][/color]
[color=blue][color=green][color=darkred]
>>>Because of this, I'd posit that strcmp() is _essentially_ a boolean
>>>function: zero equal, non-zero if different.[/color]
>>
>>It is a comparison function designed to work with qsort().
>>
>>If one want to easily detect multiple occurrences of strings
>>in an array, one way is to use qsort() along with strcmp()
>>so that these occurrences become contiguous,
>>and then any behavior of strcmp() fits the job
>>as long as it is a comparison function.[/color]
>
> I'm sure strcmp() was intended to be used directly with qsort() when
> the functions were originally designed. However, qsort() expects a
> comparison function that takes two arguments of type "const void*",
> whereas strcmp() expects arguments of type "const char*". There are
> some guarantees of compatibility between void* and char*, but I'm not
> sure they extend to compatbility of functions taking them as
> arguments.
>
> At worst, though, strcmp() can be used with qsort() with a very simple
> wrapper.[/color]
Oups. And to make things worse...
Should this extension of compatibility have existed between
functions taking "char*" and "void*" as arguments,
this would not have worked either with the example I had in mind,
that is, sorting an array of elements, each of type char *,
because another level of indirection is introduced:
The comparison function of qsort expects two pointers to the elements
be compared, as opposed to the elements themselves,
therefore here, behind the two "void *"
it expects two "const char **" as opposed to two "const char *"
int strcmp_wrapper (const void * a, const void * b)
{
return strcmp (* (const char **) a, * (const char **) b);
}
Keith Thompson wrote:[color=blue]
> regis <regis@dil.un iv-mrs.fr> writes:[color=green]
>>Rod Pemberton wrote:[/color][/color]
[color=blue][color=green][color=darkred]
>>>Because of this, I'd posit that strcmp() is _essentially_ a boolean
>>>function: zero equal, non-zero if different.[/color]
>>
>>It is a comparison function designed to work with qsort().
>>
>>If one want to easily detect multiple occurrences of strings
>>in an array, one way is to use qsort() along with strcmp()
>>so that these occurrences become contiguous,
>>and then any behavior of strcmp() fits the job
>>as long as it is a comparison function.[/color]
>
> I'm sure strcmp() was intended to be used directly with qsort() when
> the functions were originally designed. However, qsort() expects a
> comparison function that takes two arguments of type "const void*",
> whereas strcmp() expects arguments of type "const char*". There are
> some guarantees of compatibility between void* and char*, but I'm not
> sure they extend to compatbility of functions taking them as
> arguments.
>
> At worst, though, strcmp() can be used with qsort() with a very simple
> wrapper.[/color]
Oups. And to make things worse...
Should this extension of compatibility have existed between
functions taking "char*" and "void*" as arguments,
this would not have worked either with the example I had in mind,
that is, sorting an array of elements, each of type char *,
because another level of indirection is introduced:
The comparison function of qsort expects two pointers to the elements
be compared, as opposed to the elements themselves,
therefore here, behind the two "void *"
it expects two "const char **" as opposed to two "const char *"
int strcmp_wrapper (const void * a, const void * b)
{
return strcmp (* (const char **) a, * (const char **) b);
}
Comment