Suspicious pointer conversion

2.3k Views Asked by At

I got a suspicious pointer conversion error here. What might be the reason of this error?

I also initialized the code[] array globally as int *code[128]={0};

void encode(const char *s, int *out)
{
    while(*s)
    {
        out=code[*s];
        out+=strlen(code[*s++]);   //this is where i got the error.
    }
}
1

There are 1 best solutions below

0
On

When you will assign a particular type pointer variable with address of different type, such type of automatic type conversion is known as suspicious type conversion.

strlen requires const char *, while int *code[128] means code is an array of int *, so code[*s++] is a int *.

When int * is supplied to const char *, you get this error.

Generally supply int * pointer to strlen isn't a good idea because strlen will end when a byte is '\0'. You have a good chance to have 0 in a 4 bytes int. E.g., an integer 3 will have 3 bytes of 0, and 1 byte of 3.