两个警告:赋值在没有强制转换的情况下从指针生成整数,以及指针和整数之间的比较

时间:2014-05-14 06:05:13

标签: c pointers compiler-warnings

如何修复这两个警告。我已经反编译了这段代码并修改了一些但我无法摆脱这两个警告。

我想我需要将int DecryptedPacketa更改为char * DecryptedPacketavoid * DecryptedPacketa类似的内容?或者什么?

encryption.c:26: warning: assignment makes integer from pointer without a cast
encryption.c:27: warning: comparison between pointer and integer

void EncryptPacket(unsigned char *DecryptedPacket)
{
  int DecryptedPacketa;
  char *addressEncryptedPacket;
  const int packetSize = *(unsigned short *)DecryptedPacket+77;  
  unsigned char EncryptedPacket[packetSize];

    *(unsigned short *)(EncryptedPacket+77) = packetSize;
    addressEncryptedPacket = EncryptedPacket + 2;

    //Skips first 2 bytes of DecryptedPacket before looping to end of DecryptedPacket Buffer.
    //Process Byte by Byte for transformations.
    for ( DecryptedPacketa = &DecryptedPacket + 3;            //<- Warning 1
           DecryptedPacketa < &DecryptedPacket + packetSize;  //<- Warning 2
           ++DecryptedPacketa )
    {
      //... Lots of code (fake example below)...
      *addressEncryptedPacket = 123 + *(unsigned char *)DecryptedPacketa; //does encryption here
      //... Lots of code ...
    }
}

1 个答案:

答案 0 :(得分:1)

DecryptedPacketa的类型为int,如语句

中所定义
int DecryptedPacketa;

但你正在为它指定一个指针。再次,您将它与循环条件中的指针进行比较。这就是你收到这些警告的原因。

您需要做的是将DecryptedPacketa定义为unsigned char *类型并将for循环更改为

unsigned char *DecryptedPacketa;

for (DecryptedPacketa = DecryptedPacket + 2;           
     DecryptedPacketa < DecryptedPacket + packetSize;  
     ++DecryptedPacketa)

{ 
    // loop body
}

您需要在开头跳过两个字节。因此,您应该从DecryptedPacket + 2开始循环。另请注意,函数参数DecryptedPacket已经是指针。您无需向其应用address-of运算符&