I get a Suspicious pointer conversion in function main. How to get rid of this?

StackOverflow https://stackoverflow.com/questions/20920154

  •  24-09-2022
  •  | 
  •  

Question

I'm new here at stackoverflow. The title is my question. Can someone please help me on this. Thanks. I've been working on this for like 3 days.

This part of code encodes the file to a huffman code

void encode(const char *s, char *out)
{
    while (*s) {
        strcpy(out, code[*s]);
        out += strlen(code[*s++]);
    }
}    

This part of code deciphers the file from a huffman code to a human readable code

void decode(const char *s, node t)
{
    node n = t;
    while (*s) {
        if (*s++ == '0') n = n->left;
        else n = n->right;

        if (n->c) putchar(n->c), n = t;
    }

    putchar('\n');
    if (t != n) printf("garbage input\n");
}

This part is where I get my error.

int main(void)
{
    int i;
    const char *str = "this is an example for huffman encoding", buf[1024];

    init(str);
    for (i=0;i<128;i++)
        if (code[i]) printf("'%c': %s\n", i, code[i]);

    encode(str, buf); /* I get the error here */
    printf("encoded: %s\n", buf);

    printf("decoded: ");
    decode(buf, q[1]);

    return 0;
}
Was it helpful?

Solution

Declare 'buf' in a different line, and not as 'const':

char buf[1024];

OTHER TIPS

The const applies to all the declarations on the line, so you're declaring buf as a const char[1024]. That means that calling encode casts away the constness, resulting in the warning.

Avoid having multiple variable declarations on the same line, unless they are all exactly the same type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top