0

当我尝试在带有-Wall标志的 linux 上使用 gcc 版本 4.6.3 编译此代码时,我收到以下两个警告:

  1. 它指向cmd[1]=cmd2send;

    警告:赋值使指针从整数而不进行强制转换[默认启用]

  2. 它指向变量static unsigned char *cmd[65]

    警告:变量 'cmd' 已设置但未使用 [-Wunused-but-set-variable]。

是什么导致了这些警告?以及如何避免它们?

  int CommandHandler(unsigned char cmd2send)
    {
        static unsigned char *cmd[65];

        // *make sure device is open*
        if(handle==NULL) // handle defined in transceiver.h
        {
            puts("CommandHandler::Cant handle command");
            // try to open device again
            if(OpenMyDev()!=0)
            return -1;
            // if no return then retry is fine
            puts("retry SUCCEEDED!, device is open");
        }

        // *send command to firmware*
        cmd[0]=0x0;
        cmd[1]=cmd2send;
        .......
        return 0;

    }

在这里,我也收到关于可变电压的警告 #2:

float Get_Temperature(void)
{
    //unsigned char RecvByte=0;
    //int byte[4];

    int i;

    float voltage=0;
    float resistance=0;
    float temperature=0;
    float SamplesAVG=0;

    unsigned int Samples=0;

    unsigned char* rvc;
    unsigned char mydata[65];


    for(i=0;i<=10;i++)
    {
        //Transimit Start Of Frame
        mydata[0]=0;
        mydata[1]=GET_TEMPERATURE;

        if(Write2MyDev(mydata)<0)  {return -1;}

        rvc=ReadMyDev(0);

        SamplesAVG+=(rvc[0]<<24)+(rvc[1]<<16)+(rvc[2]<<8)+rvc[3];
        usleep(100*1000);
    }

    Samples=SamplesAVG/10;
    printf("TO PRINT VAL:%d\n",Samples);
    puts("------------");

    voltage = (Samples * 5.0)/1023.0; // 0..1203= 1024 values
    resistance = 10000.0/ (1023.0/Samples);
    ...
    return retval;
}
4

2 回答 2

4

cmd2send是 aunsigned char并且您正在为其设置cmd[1]is a的值char *。Achar *是一个指针,并且 anunsigned char被视为一个整数,因此您正在cmd[1]integer没有强制转换的 an 中创建一个指针。

您可能想要一个字符数组而char cmd[65]不是char *

此外,由于您创建并分配了值cmd但您从未使用它,因此您也会收到警告。

于 2012-04-06T18:36:17.047 回答
3
static unsigned char *cmd[65];
....
cmd[1] = cmd2send; /* cmd is an array of pointers so cmd[1] is a pointer. */

好的,cmd数组 pf 256 指针也是如此。分配 achar生成cmd[1]第一个警告。

编译器还注意到您实际上并没有cmd在任何地方使用,因此它会生成第二个警告。

于 2012-04-06T18:34:42.263 回答