Pointer cast

Asked

Viewed 54 times

-1

void    *ft_memchr(const void *s, int c, size_t n)
{
    while (n > 0)
    {
        if (*(unsigned char *) s == c)
        {
            return ((void *)s);
        }
        s++;
        n--;
    }
    return (NULL);
}

What would this be *(unsigned char *)s?

I understand it’s a cast because it’s a void and such, but what I can’t understand and can’t find material to understand is these pointers, for example the first * I no longer know what it is, but from what I’ve seen its existence it already leaves me totally in doubt of the existence of the second pointer.

1 answer

0


The (unsigned char *) is a cast from any address to an unsigned char type address*.

A unsigned char has a byte size.

The first * is to de-reference the address of s.

As the address of s is now interpreted as a unsigned char will be de-referenced one byte from the address.

And this byte is compared with the variable c.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.