Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用字符执行异或字节比较_C_Byte_Xor - Fatal编程技术网

C 使用字符执行异或字节比较

C 使用字符执行异或字节比较,c,byte,xor,C,Byte,Xor,我试图在程序中的无符号字符数组中每3个字节执行一次异或字节操作。我试图编译程序时发生类型转换错误 我的声明 unsigned char *s; FILE *fp; char ch; int count = 0; unsigned char *load = malloc(sizeof(char)); int i = 0; s = "s"; 这就是错误发生的地方 for (i = 3; i < count;) { temp = (load[i-1] ^ s); temp2

我试图在程序中的无符号字符数组中每3个字节执行一次异或字节操作。我试图编译程序时发生类型转换错误

我的声明

unsigned char *s;
FILE *fp;
char ch;
int count = 0;
unsigned char *load = malloc(sizeof(char));
int i = 0;

s = "s";
这就是错误发生的地方

for (i = 3; i < count;)
{
    temp = (load[i-1] ^ s);
    temp2 = (load[i] ^ s);
            i = i + 3;
}       
这是它产生的错误

main.c:14:4:警告:将“char[2]”赋值给“unsigned char*”,将在指针之间转换为 具有不同符号[-Wpointer符号]的整数类型

    s = "s";
      ^ ~~~
main.c:86:21:错误:二进制表达式('int'和'unsigned char*'的操作数无效)

main.c:87:20:错误:二进制表达式('int'和'unsigned char*'的操作数无效)


您正在分配内存,以便在内存中仅保存一个
无符号字符

unsigned char *load = malloc(sizeof(char));
然后您尝试使用
load[i-1]
访问第三个字符

更新

编译器错误非常清楚错误的性质

main.c:86:21: error: invalid operands to binary expression ('int' and 'unsigned char *')

        temp = (load[i-1] ^ s);
也许你想用:

        temp = (load[i-1] ^ s[0]);
关于另一个编译器消息,您可以在定义它时初始化
s

unsigned char *s = "S";

而不是以后分配给它。

您能对您看到的问题进行最低限度的说明吗?目前,这段代码没有任何意义,例如,因为没有定义temp和temp2;char temp,temp2是temp的声明方式。unsigned char*load=malloc(sizeof(char));可以处理while((ch=fgetc(fp))!=EOF){load[i++]=ch;}您很好地说明了问题(其中一个)。但不确定OP的问题是否得到了明确的回答。OP的问题没有得到明确的回答,因为
temp
temp2
的类型未知。我编辑了这篇文章以提供更清晰的信息,希望这会有所帮助。谢谢,这解决了大多数编译问题。这是使用
s[0]的另一种可能更简单的解决方案
定义为:
无符号字符s='s'
main.c:86:21: error: invalid operands to binary expression ('int' and 'unsigned char *')

        temp = (load[i-1] ^ s);
        temp = (load[i-1] ^ s[0]);
unsigned char *s = "S";