Invalid operands to binary -

  • Thread starter Spiros Bousbouras
  • Start date
S

Spiros Bousbouras

#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.
 
E

Eric Sosman

Spiros said:
#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?
 
R

Robert Gamble

#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
 
S

Spoon

Spiros said:
#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 -
 
S

Spiros Bousbouras

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.
 
S

Spiros Bousbouras

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.
 
R

Robert Gamble

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
 
C

CBFalconer

Spiros said:
#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.
 
E

Eric Sosman

Eric Sosman wrote On 08/08/07 08:05,:
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.
 
C

CBFalconer

Robert said:
.... snip ...


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.
 
R

Robert Gamble

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
 
M

Martin Ambuhl

Spiros said:
#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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top