AW: [PATCH v2] tsearch.3: Simplify type usage and remove unneeded casts

Walter Harms
2020-09-07T07:54:34Z

Hello,
i am sorry to interrupt here but ...
IMHO the void *val is here for a reason, because it means
"this can be anything" the reason why int ** works here is
that the example uses int. You make the example to specific
in this case. may be the example from bsearch is better here.

________________________________________

Von: linux-man-owner@vger.kernel.org [linux-man-owner@vger.kernel.org] im Auftrag von Michael Kerrisk (man-pages) [mtk.manpages@gmail.com]
Gesendet: Samstag, 5. September 2020 21:42
An: Alejandro Colomar
Cc: mtk.manpages@gmail.com; linux-man@vger.kernel.org
Betreff: Re: [PATCH v2] tsearch.3: Simplify type usage and remove unneeded casts

Hello Alex,

On 9/5/20 6:10 PM, Alejandro Colomar wrote:
> The type of `val` is `int **`, and it will work with tsearch()
> anyway because of implicit cast from `void *`, so declaring it as an
> `int **` simplifies the code.

Thanks, patch applied.

Cheers,

Michael

> Signed-off-by: Alejandro Colomar <colomar.6.4.3@gmail.com>
> ---
> man3/tsearch.3 | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/man3/tsearch.3 b/man3/tsearch.3
> index 2e8403130..7b82d9bd3 100644
> --- a/man3/tsearch.3
> +++ b/man3/tsearch.3
> @@ -323,7 +323,7 @@ action(const void *nodep, VISIT which, int depth)
> int
> main(void)
> {
> - void *val;
> + int **val;
>
> srand(time(NULL));
> for (int i = 0; i < 12; i++) {
> @@ -332,7 +332,7 @@ main(void)
> val = tsearch((void *) ptr, &root, compare);
> if (val == NULL)
> exit(EXIT_FAILURE);
> - else if ((*(int **) val) != ptr)
> + else if (*val != ptr)
> free(ptr);
> }
> twalk(root, action);
>

--
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/