如何解决这两个警告。我已经对该代码进行了反编译,并对其进行了一些修复,但是我无法摆脱这两个警告。
我想我需要将int DecryptedPacketa
更改为喜欢char * DecryptedPacketa
或void * 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 ...
}
}
最佳答案
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
运算符&
。