Pointer targets in passing argument differ in signedness

ogs picture ogs · Jul 30, 2015 · Viewed 37.9k times · Source

I've read through similar questions, but I've not been able to find one that helps me understand this warning in this case. I'm in my first week of trying to learn C, so apologies in advance.

I get the following warning and note:

In function 'read_line':
warning: pointer targets in passing argument 1 of 'read_byte' differ in signedness [-Wpointer-sign]
   res = read_byte(&data);  
   ^
note: expected 'char *' but argument is of type 'uint8_t *'
 char read_byte(char * data) 

When trying to compile this code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>

char read_byte(char * data) 
{
    if(fs > 0 )
    {
        int n = read(fs, data, 1);
        if (n < 0)
        {
            fprintf(stderr, "Read error\n");
            return 1;
        }
    }
    return *data;
}

uint8_t read_line(char * linebuf) 
{
    uint8_t data, res;
    char * ptr = linebuf;

    do
    {
        res = read_byte(&data);         
        if( res < 0 )
        {
            fprintf(stderr, "res < 0\n");
            break;
        }

        switch ( data )
        {
            case '\r' :
                break;
            case '\n' : 
                break;
            default : 
                *(ptr++) = data;
                break;
        }

    }while(data != '\n');
    *ptr = 0;               // terminaison
    return res;
}

int main(int argc, char **argv)
{
    char buf[128];

    if( read_line(buf) == 10 )
    {
        // parse data
    }

    close(fs);
    return 0;
}

I removed useless part including the one which opens the port and initializes fs.

Answer

Eugene Sh. picture Eugene Sh. · Jul 30, 2015

char is signed type. uint8_t is unsigned. So you are passing a pointer to an unsigned type to a function requiring signed. You have several options:

1) Change the function signature to accept uint8_t* instead of char*

2) Change the type of parameter you are passing to char* instead of uint8_t* (i.e. change data to be char).

3) Perform an explicit cast when calling the function (the less preferable option).

(Or ignore the warning, which I don't include as an option, considering it wrong)