2

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.

ogs
  • 1,139
  • 8
  • 19
  • 42
  • if `res` is short for `result`, then why not use `int`s and return TRUE or FALSE? Seems much clearer to me. Currently yiou have inconsistent use of `res` and `data`. – Paul Ogilvie Jul 30 '15 at 17:18
  • Indeed, ( res < 0 ) is useless.. But in which cases return TRUE or FALSE ? – ogs Jul 30 '15 at 17:27

2 Answers2

11

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)

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
  • 1
    Actually, you *can* safely ignore the warning. The C standard allows accessing *any* type through a pointer to character type. – EOF Jul 30 '15 at 19:25
  • @EOF I agree, but doing so may develop into a bad habit. – Eugene Sh. Jul 30 '15 at 19:27
  • 1
    The only thing I'd recommend is casting the `uint8_t *` to `void*` before passing it to a function expecting a `char*`. – EOF Jul 30 '15 at 19:27
  • 1
    Can you show an example of how the cast would be done? – Aaron Mar 21 '20 at 08:04
1

You are sending the address of type uint8_t

res = read_byte(&data);

And receiving it as char *

char read_byte(char * data) 
Haris
  • 12,120
  • 6
  • 43
  • 70