473,805 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16946
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.c omwrote:
#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.invalidwrot e:
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...@gmai l.comwrote:
On Aug 8, 7:42 am, Spiros Bousbouras <spi...@gmail.c omwrote:


#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.c omwrote:
On Aug 8, 12:07 pm, Robert Gamble <rgambl...@gmai l.comwrote:
On Aug 8, 7:42 am, Spiros Bousbouras <spi...@gmail.c omwrote:
#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.c omwrote:
.... 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
31391
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) >= NSIG) /* <signal.husually defines NSIG to include signal number 0 */
1
2772
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
29711
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
7139
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 *nodelist, struct
36
4059
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 it mean. how to correct fault? please help i'm only new to C. #include<stdio.h> #include<math.h> main()
3
31347
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 operation in C?
3
2619
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
5808
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 _granx1;
2
4998
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 ()(double)' and `double' to binary `operator*' Please Help thanks. #include <iostream> #include <iomanip> #include <cmath>
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.