0
votes

I'm trying to reproduce the function memcmp, and when i try to compile i got the error:

error: assignment makes integer from pointer without a cast [-Werror]
str1 = (unsigned char*)s1

Here is my code :

int ft_memcmp(const void *s1, const void *s2, size_t n)
{
    unsigned char   str1;
    unsigned char   str2;

    str1 = (unsigned char*)s1;
    str2 = (unsigned char*)s2;
    while (n--)
    {
        if (str1 != str2)
           return (str1 - str2);
        str1++;
        str2++;
    }
    return (0);
}

can anyone help me with those cast ? i really don't understand why it doesn't work

1

1 Answers

3
votes

You want str1 and str2 to be pointers (matching the cast), not unsigned chars:

unsigned char *str1, *str2;

str1 = (unsigned char*)s1;
str2 = (unsigned char*)s2;

while (n--)
{
  if (*str1 != *str2)
    return (*str1 - *str2);

  str1++;
  str2++;
}