Sarath Chandra
Sarath Chandra

Reputation: 13

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

The code that does this is the following, where I'm using typedef struct pointer and #define (to get to know the usage).

#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;
}

Upvotes: 0

Views: 782

Answers (1)

kiran Biradar
kiran Biradar

Reputation: 12732

You are trying to cast int to str *

((str*)I->rs)

Here you are casting I->rs to str * but you meant.

 ((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))

Upvotes: 2

Related Questions