Understanding memset c

66 views Asked by At

I'm trying to understand how memset works

Here is my implementation :

void *ft_memset(void *s, int c, size_t n)
{
    size_t i;

    i = 0;
    while (i < n)
    {
        *((char *)(s + i)) = c;
        i++;
    }
    return (s);
}

Why does memset takes void as parameter since it doesn't work with ints, is there more than char that it's used for ? Why casting to char * makes this work for every case scenario ?

1

There are 1 answers

0
xTurtho On

I did a little research. It seems this function uses void to permit the use of signed char, unsigned char and char. So the typecasting now makes sense since the edited values will be of type char.