C语言 传递参数中的指针目标的符号不同

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31730084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:03:36  来源:igfitidea点击:

Pointer targets in passing argument differ in signedness

cpointerswarningssignedness

提问by ogs

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.

我已经阅读了类似的问题,但在这种情况下,我无法找到可以帮助我理解此警告的问题。我正在尝试学习 C 的第一周,所以提前道歉。

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.

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

回答by Eugene Sh.

charis signed type. uint8_tis unsigned. So you are passing a pointer to an unsigned type to a function requiring signed. You have several options:

char是有符号类型。uint8_t未签名。因此,您将指向无符号类型的指针传递给需要有符号的函数。您有多种选择:

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

1)将函数签名更改为接受uint8_t*而不是char*

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

2)更改您要传递的参数类型char*而不是uint8_t*(即更改datachar)。

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

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

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

(或者忽略警告,我没有将其作为选项包括在内,认为它是错误的)

回答by Haris

You are sending the address of type uint8_t

您正在发送类型的地址 uint8_t

res = read_byte(&data);

And receiving it as char *

并将其作为 char *

char read_byte(char * data)