Weird typing error not seen by gcc. 
Author Message
 Weird typing error not seen by gcc.

Dear colleagues,

I had struggled with a program related to my work and found, finally,
the error: a line that should be "np = ( k * nj + j ) * ni + i" was
typed "np = ( k * nj + j ) * ni, + i". No warnings issued in
compilation (gcc 2.95, Conectiva Linux 7).
        ^

Do this misplaced "," have any special meaning about wich Im unaware?
I cant figure why the parsing didnt issue a warning on this.

Please, no flames. This question is being motivated by my anger with
myself, since this was a very stupid typing error that caused me a lot
of headaches.

Very thanks in advance,
HCB



Sun, 26 Dec 2004 01:48:09 GMT  
 Weird typing error not seen by gcc.


Quote:
> Dear colleagues,

> I had struggled with a program related to my work and found, finally,
> the error: a line that should be "np = ( k * nj + j ) * ni + i" was
> typed "np = ( k * nj + j ) * ni, + i". No warnings issued in
> compilation (gcc 2.95, Conectiva Linux 7).
>         ^

> Do this misplaced "," have any special meaning about wich Im unaware?

Yes, the comma in that expression denotes the comma operator.

Quote:
> I cant figure why the parsing didnt issue a warning on this.

Because it is a valid expression, yielding a value of i.


Sun, 26 Dec 2004 01:54:53 GMT  
 Weird typing error not seen by gcc.

Quote:
> I had struggled with a program related to my work and found, finally,
> the error: a line that should be "np = ( k * nj + j ) * ni + i" was
> typed "np = ( k * nj + j ) * ni, + i". No warnings issued in
> compilation (gcc 2.95, Conectiva Linux 7).

You have just discovered the comma operator, which evaluates its
left-hand operand and throws away its value, then evaluates its
right-hand operand, which becomes the value of the expression.

`+i' is valid for the same reason `-i' would be valid.  It is the
unary plus operator.



Sun, 26 Dec 2004 01:51:50 GMT  
 Weird typing error not seen by gcc.
* Helio C. Bortolon
| I had struggled with a program related to my work and found, finally,
| the error: a line that should be "np = ( k * nj + j ) * ni + i" was
| typed "np = ( k * nj + j ) * ni, + i". No warnings issued in
| compilation (gcc 2.95, Conectiva Linux 7).
|         ^
|
| Do this misplaced "," have any special meaning about wich Im unaware?
| I cant figure why the parsing didnt issue a warning on this.

It's the comma operator: a binary operator which evaluates both its
operands and yields the value of the second.  For instance:

  int i = 1, j = 2, k;
  k = j++, i;

After the second line is executed j has been incremented and i's value
has been assigned to k.

You also inadvertently used the (pointless) unary '+' operator, which
yields the value of its operand.

Jeremy.



Sun, 26 Dec 2004 02:05:21 GMT  
 Weird typing error not seen by gcc.

Quote:

> Dear colleagues,

> I had struggled with a program related to my work and found, finally,
> the error: a line that should be "np = ( k * nj + j ) * ni + i" was
> typed "np = ( k * nj + j ) * ni, + i". No warnings issued in
> compilation (gcc 2.95, Conectiva Linux 7).

Well, gcc is correct. You have just typed
   np = ( k * nj + j ) * ni
and
   + i
separated by the comma operator.

So the part before the comma is evaluated first, the +i then.
The value returned by the unary +, which is actually the value of i
is returned by the comma operator and then thrown away.
It's arguable, wether a diagnotic should be supplied on that account.
It's not required, though.

Jirka



Sun, 26 Dec 2004 02:07:16 GMT  
 Weird typing error not seen by gcc.


Quote:

>   int i = 1, j = 2, k;
>   k = j++, i;

> After the second line is executed j has been incremented and i's value
> has been assigned to k.

No, the value of j is assigned to k. The expression k=j++,i is parsed as
(k=j++),(i)


Sun, 26 Dec 2004 02:08:33 GMT  
 Weird typing error not seen by gcc.

Quote:

> Dear colleagues,

> I had struggled with a program related to my work and found, finally,
> the error: a line that should be "np = ( k * nj + j ) * ni + i" was
> typed "np = ( k * nj + j ) * ni, + i". No warnings issued in
> compilation (gcc 2.95, Conectiva Linux 7).
>         ^

> Do this misplaced "," have any special meaning about wich Im unaware?
> I cant figure why the parsing didnt issue a warning on this.

In this context the , is the ... comma operator! (As oposed to the , that
separates function parameters).
It's perfectly legal: You can build an expression by separating other
expressions with ,. The value of the comma expression is the value of the
last expression (in your case +i). There is a sequence point at each ,. I
use it mainly in for(;;) statements: for(a=0 , b=0 ; ; ).
I'm not sure why gcc -Wunused-value (included in -Wall) doesn't emmit a
warning here.
Tobias.

--
unix http://www.faqs.org/faqs/by-newsgroup/comp/comp.unix.programmer.html
clc http://www.eskimo.com/~scs/C-faq/top.html
fclc (french): http://www.isty-info.uvsq.fr/~rumeau/fclc/



Sun, 26 Dec 2004 02:00:45 GMT  
 Weird typing error not seen by gcc.

Quote:



> >   int i = 1, j = 2, k;
> >   k = j++, i;

> > After the second line is executed j has been incremented and i's value
> > has been assigned to k.

>  No, the value of j is assigned to k. The expression k=j++,i is parsed as
>  (k=j++),(i)

Of course.  Sorry.


Sun, 26 Dec 2004 02:18:18 GMT  
 Weird typing error not seen by gcc.

Quote:

> I had struggled with a program related to my work and found, finally,
> the error: a line that should be "np = ( k * nj + j ) * ni + i" was
> typed "np = ( k * nj + j ) * ni, + i". No warnings issued in
> compilation (gcc 2.95, Conectiva Linux 7).
>         ^

> Do this misplaced "," have any special meaning about wich Im unaware?
> I cant figure why the parsing didnt issue a warning on this.

On a related note, one of the most annoying errors I have faced of a
similar kind was inside a switch statement where I typed "case1:" instead
of "case 1:". This was in an unenlightened editor that didn't do a lot of
automatic indentation magic like (x)emacs, so it was really hard to find
out that I had accidentally converted my intended case into a label.

-nrk.



Sun, 26 Dec 2004 07:56:53 GMT  
 Weird typing error not seen by gcc.

Quote:

> On a related note, one of the most annoying errors I have faced of a
> similar kind was inside a switch statement where I typed "case1:" instead
> of "case 1:". This was in an unenlightened editor that didn't do a lot of
> automatic indentation magic like (x)emacs, so it was really hard to find
> out that I had accidentally converted my intended case into a label.

Hmm.  I just got a wonderful idea for my next "Just another C
hacker" signature.
--
"...deficient support can be a virtue.
 It keeps the amateurs off."
--Bjarne Stroustrup


Sun, 26 Dec 2004 14:34:10 GMT  
 Weird typing error not seen by gcc.

Quote:
>I had struggled with a program related to my work and found, finally,
>the error: a line that should be "np = ( k * nj + j ) * ni + i" was
>typed "np = ( k * nj + j ) * ni, + i". No warnings issued in
>compilation (gcc 2.95, Conectiva Linux 7).
>        ^

>Do this misplaced "," have any special meaning about wich Im unaware?
>I cant figure why the parsing didnt issue a warning on this.

>Please, no flames. This question is being motivated by my anger with
>myself, since this was a very stupid typing error that caused me a lot
>of headaches.

If your C book doesn't mention the comma operator, you should ask for a
refund.  If it does, you have one more reason to be angry with yourself :-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sun, 26 Dec 2004 20:17:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. gcc g++: File.h for gcc - did you ever seen

2. Weird build error, weird makefile error, flaky VC4.0

3. Same property name and type gives weird error by compiler

4. The weirdest bug I have ever seen

5. HELP-Conflicting types and casting float to NULL errors on GCC 2.7.0 Linux

6. Anyone seen this: fatal error CS0013: Unexpected error writing metadata to file

7. weird gcc type error problem... any ideas?

8. weird, weird and really weird?

9. Seeing an interface in a type library

10. Custom UserControl enum type cannot be seen by designer

11. WEIRD macro bug only when compiling with GCC

12. multiple getchar()'s in gcc (weird)

 

 
Powered by phpBB® Forum Software