简体   繁体   English

错误:二进制<<的无效操作数(具有“ struct str *”和“ int”)

[英]error: invalid operands to binary << (have ‘struct str *’ and ‘int’)

How do I make sure that the following program does not cause these errors? 如何确保以下程序不会导致这些错误?

warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]    
warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]    
error: invalid operands to binary << (have ‘struct EXT_HDR *’ and ‘int’)

My expected output is: 15 我的预期输出是: 15

The code that does this is the following, where I'm using typedef struct pointer and #define (to get to know the usage). 下面是执行此操作的代码,在这里我使用typedef struct指针和#define(以了解用法)。

#include <stdio.h>

typedef struct EXT_HDR {
        int sar,rs;
}str;

#define output(O,I) (O |= ((str*)I->sar) | (((str*)I->rs)<<2))
int main(){

        int out = 0;
        str* val;
        val->sar = 3;
        val->rs = 3;
        output(out,val);
        printf("output= %d\n",out);
        return 0;
}

You are trying to cast int to str * 您正在尝试将intstr *

((str*)I->rs)

Here you are casting I->rs to str * but you meant. 在这里,您将I->rs强制转换为str *但这是您的意思。

 ((str*)I)->rs

change 更改

#define output(O,I) (O |= ((str*)I->sar) | (((str*)I->rs)<<2))

to

#define output(O,I) (O |= (((str*)I)->sar) | (((str*)I)->rs<<2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二进制 + 的无效操作数(具有 'struct student' 和 'int') - invalid operands to binary + (have 'struct student' and 'int') 错误:二进制+的无效操作数(有'int *'和'int *')| - error: invalid operands to binary + (have 'int *' and 'int *')| 错误:二进制 * 的无效操作数(有 'int' 和 'int *') - error: invalid operands to binary * (have 'int' and 'int *') 错误:二进制 + 的操作数无效(有 'int *' 和 'int *') - error: invalid operands to binary + (have ‘int *’ and ‘int *’) 二进制 &amp; 的错误操作数无效(有 'int **' 和 'int *') - Error invalid operands to binary & (have 'int **' and 'int *') 错误:二进制 * 的无效操作数(有 'int *' 和 'int') - error: invalid operands to binary * (have ‘int *’ and ‘int’) 错误:二进制 % 的操作数无效(有“int”和“double”) - error: invalid operands to binary % (have 'int' and 'double') 错误:二进制 ^ 的无效操作数(有 &#39;float&#39; 和 &#39;int&#39; ) - Error : invalid operands to binary ^ (have 'float' and 'int' ) 错误:二进制操作数无效&gt;&gt;(具有“float”和“int”) - error: invalid operands to binary >> (have 'float' and 'int') 将错误无效操作数编译为二进制 &amp;&amp;(具有 'int' 和 'pthread_t' {aka 'struct<anonymous> '})</anonymous> - Compiling error invalid operands to binary && (have 'int' and 'pthread_t' {aka 'struct <anonymous>'})
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM