传递参数的指针目标的符号不同



我已经阅读了类似的问题,但是我没有找到一个可以帮助我理解这种情况下的警告的问题。这是我尝试学习C语言的第一周,所以提前道歉。

我得到以下警告和注意:

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) 

当尝试编译此代码时:

#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 errorn");
            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 < 0n");
            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;
}

我删除了无用的部分,包括打开端口和初始化fs的部分。

char为有符号类型。uint8_t是无符号的。因此,您将指向unsigned类型的指针传递给需要signed的函数。您有几个选项:

1)修改函数签名接受uint8_t*而不是char*

2)更改传递给char*的参数类型,而不是uint8_t*(即将data更改为char)。

3)调用函数时执行显式强制类型转换(不太可取的选项)。

(或者忽略警告,我没有把它作为一个选项,认为它是错误的)

您正在发送类型为uint8_t的地址

res = read_byte(&data);

接收为char *

char read_byte(char * data) 

最新更新