By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,959 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,959 IT Pros & Developers. It's quick & easy.

Invalid operands to binary -

P: n/a
#include <stdlib.h>

int main(void) {
char **p1 ;
const char **p2 ;

p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;
}

When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -

Is it a compiler bug ? It seems legal to me.

Aug 8 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Spiros Bousbouras wrote:
#include <stdlib.h>

int main(void) {
char **p1 ;
const char **p2 ;

p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;
}

When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -

Is it a compiler bug ? It seems legal to me.
I see nothing wrong with it. Try a gcc forum
to see whether it's a known bug?

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 8 '07 #2

P: n/a
On Aug 8, 7:42 am, Spiros Bousbouras <spi...@gmail.comwrote:
#include <stdlib.h>

int main(void) {
char **p1 ;
const char **p2 ;

p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;

}

When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -

Is it a compiler bug ?
No, it's almost never a compiler bug.

Pointer subtraction occurs between compatible pointers, p1 and p2 are
incompatible types and cannot participate in pointer subtraction. You
should have received a warning on line 9 as well as you need a cast to
make the assignment work.

Robert Gamble

Aug 8 '07 #3

P: n/a
Spiros Bousbouras wrote:
#include <stdlib.h>

int main(void) {
char **p1 ;
const char **p2 ;

p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;
}

When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -
gcc 3.4.4 said:

foo.c: In function `main':
foo.c:9: warning: assignment from incompatible pointer type
foo.c:10: error: invalid operands to binary -
Aug 8 '07 #4

P: n/a
On Aug 8, 12:05 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Spiros Bousbouras wrote:
#include <stdlib.h>
int main(void) {
char **p1 ;
const char **p2 ;
p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;
}
When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -
Is it a compiler bug ? It seems legal to me.

I see nothing wrong with it. Try a gcc forum
to see whether it's a known bug?
I did try Googling for "invalid operands to binary -"
in several gcc forums but I didn't find anything. I also
went to the gcc bugs page (gcc.gnu.org/bugs.html)
but once again nothing turned up.
Aug 8 '07 #5

P: n/a
On Aug 8, 12:07 pm, Robert Gamble <rgambl...@gmail.comwrote:
On Aug 8, 7:42 am, Spiros Bousbouras <spi...@gmail.comwrote:


#include <stdlib.h>
int main(void) {
char **p1 ;
const char **p2 ;
p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;
}
When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -
Is it a compiler bug ?

No, it's almost never a compiler bug.

Pointer subtraction occurs between compatible pointers, p1 and p2 are
incompatible types and cannot participate in pointer subtraction. You
should have received a warning on line 9 as well as you need a cast to
make the assignment work.
I don't believe they are incompatible but rather p2 is
a qualified (with const) version of the type of p1. In
any case I don't see why the standard should forbid such
a thing ; it seems an unreasonable rstriction.

Aug 8 '07 #6

P: n/a
On Aug 8, 8:49 am, Spiros Bousbouras <spi...@gmail.comwrote:
On Aug 8, 12:07 pm, Robert Gamble <rgambl...@gmail.comwrote:
On Aug 8, 7:42 am, Spiros Bousbouras <spi...@gmail.comwrote:
#include <stdlib.h>
int main(void) {
char **p1 ;
const char **p2 ;
p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;
}
When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -
Is it a compiler bug ?
No, it's almost never a compiler bug.
Pointer subtraction occurs between compatible pointers, p1 and p2 are
incompatible types and cannot participate in pointer subtraction. You
should have received a warning on line 9 as well as you need a cast to
make the assignment work.

I don't believe they are incompatible but rather p2 is
a qualified (with const) version of the type of p1.
You are confused, p2 is not const qualified, it is a "pointer to
pointer to const char", which is a completely distinct and
incompatible type from "pointer to pointer to char". If the pointers
were defined as:

char ** p1;
char ** const p2;

Then you would be correct as p2 would now be a "const pointer to
pointer to char" (the const qualifier being applied to the pointer
itself) thus being a "qualified version of a compatible type".

Check out questions 11.9 and 11.10 on the FAQ <http://www.c-faq.com/>
for more details.

Robert Gamble

Aug 8 '07 #7

P: n/a
Eric Sosman wrote On 08/08/07 08:05,:
Spiros Bousbouras wrote:
>>#include <stdlib.h>

int main(void) {
char **p1 ;
const char **p2 ;

p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;
}

When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -

Is it a compiler bug ? It seems legal to me.


I see nothing wrong with it. Try a gcc forum
to see whether it's a known bug?
Seems I didn't see clearly enough ... Thanks
to Robert Gamble for the correction.

--
Er*********@sun.com
Aug 8 '07 #8

P: n/a
Spiros Bousbouras wrote:
>
#include <stdlib.h>

int main(void) {
char **p1 ;
const char **p2 ;

p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;
}

When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -

Is it a compiler bug ? It seems legal to me.
IMO it is a misapplied bug. The initialization of p2 cannot
succeed, because it is a const, and can only be set at declaration
time. Gcc apparently remembers that it has invalid content, and
thus rejects the arithmetic statement.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 8 '07 #9

P: n/a
Robert Gamble wrote:
On Aug 8, 8:49 am, Spiros Bousbouras <spi...@gmail.comwrote:
.... snip ...
>
>I don't believe they are incompatible but rather p2 is
a qualified (with const) version of the type of p1.

You are confused, p2 is not const qualified, it is a "pointer to
pointer to const char", which is a completely distinct and
incompatible type from "pointer to pointer to char". If the
Me too. Scrub my earlier answer.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 8 '07 #10

P: n/a
On Aug 8, 10:25 am, CBFalconer <cbfalco...@yahoo.comwrote:
Spiros Bousbouras wrote:
#include <stdlib.h>
int main(void) {
char **p1 ;
const char **p2 ;
p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;
}
When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -
Is it a compiler bug ? It seems legal to me.

IMO it is a misapplied bug. The initialization of p2 cannot
succeed, because it is a const, and can only be set at declaration
time.
First off, p2 is not const. Second, initialization can only occur at
definition time, afterwards it becomes assignment.
Gcc apparently remembers that it has invalid content, and
thus rejects the arithmetic statement.
No, gcc rejects the expression because the pointers are not
compatible.

Robert Gamble

Aug 8 '07 #11

P: n/a
Spiros Bousbouras wrote:
#include <stdlib.h>

int main(void) {
char **p1 ;
const char **p2 ;

p1 = malloc(5 * sizeof(char *)) ;
if (p1 == 0) return EXIT_FAILURE ;
p2 = p1 + 1 ;
p2 - p1 ;
return 0 ;
}

When I try to compile the above on gcc 4.1.2 I get
t.c: 10: error: invalid operands to binary -

Is it a compiler bug ? It seems legal to me.
The actual problem is for the line above:
p2 = p1 + 1;
In fact, my copy of gcc issues a diagnostic for that line, which
diagnostic you are not reporting.

What gcc doesn't like is that 'const char **' and 'char **' are not
compatible types. If you remove const from the declaration of p2, both
of those diagnostics will go away.

I'll leave it to others to discuss whether 'const char **' and 'char **'
are compatible types, or why they should be, or why they are not.
Aug 8 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.