473,246 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

Invalid operands to binary -

#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
11 16873
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: muthu | last post by:
In the following code it gives the error "error: invalid operands to binary &" Why it is happening #include <signal.h> #include <errno.h> #define SIGBAD(signo) ((signo) <= 0 || (signo) >=...
1
by: Richard Eich | last post by:
gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-3) source snippet: .... int i = 17 ; if ( 0x03 & i ) ....
2
by: xelloss | last post by:
#include<cstdlib> #include<iostream> #include<iomanip> #include<vector> #include<fstream> using namespace std; double sum(vector<double> x) { double total = 0.0;
5
by: Sheldon | last post by:
Hi Everyone, I have defined a function: struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct Transient retv); and in the code: struct Transient arrFromHdfNode(HL_NodeList...
36
by: Kapteyn's Star | last post by:
hi group, i try to compile code below but my compiler is failing. it tells:- printbin.c: In function ‘main’: printbin.c:9: error: invalid operands to binary & i am not able to understand what...
3
by: triphoppa | last post by:
I'm a little confused about and error my compiler is giving me. The error message is "invalid operand to binary %." The compiler says that this code *avg = *range %10; Isn't this a valid binary...
3
by: Shantanu Godbole | last post by:
temp1 = number%pow(10,i)/pow(10,i-1); An error is shown in this line even though i include math.h library and use the correct operators ERROR: invalid binary operands to % How to deal with it?
2
by: Genro | last post by:
#include<stdio.h> #include<TX/graphics.h> #include<time.h> // I need help! struct Krug{ double _x; double _y; double _skox; double _skoy; double...
2
by: Tyler Palmer | last post by:
I am having a problem with my program. I cant figure out a solution for the compiler error im getting. In function `double endingConversion(double)': invalid operands of types `double...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.