Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
859 views
in Technique[技术] by (71.8m points)

c - error: invalid operands to binary * (have ‘int *’ and ‘int’)

I am trying to convert a number in IP address using pointers in C.

for example: IP = 3229305093 192.123.73.5

and the error i have is:

error: invalid operands to binary * (have ‘int *’ and ‘int’)
bytes[i] = (*ip1 >> (int )i8) & 0xFF;

Here is my function:

void print_ip(int* ip1) {
unsigned char bytes[4];
int i;

for(i=0; i<4; i++) {
  bytes[i] = (*ip1 >> (int *)i*8) & 0xFF;
}

printf("%d.%d.%d.%d
", bytes[3], bytes[2], bytes[1], bytes[0]);

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Both operators of bit-shift operator >> should be integers. The extra cast should be removed.

Wrong:

  bytes[i] = (*ip1 >> (int *)i*8) & 0xFF;

Correct:

  bytes[i] = (*ip1 >> i*8) & 0xFF;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...