[comp.lang.c++] operator precedence question

baud@gt-eedsp.UUCP (Kurt Baudendistel) (07/19/88)

in the following code fragment,

foo(char *s, char *t) {
  char *r = "hello, there";
  ...
  for ( int i=0, t=s; i < 10; i++ ) *t++ = *r++;
  ...

your initial reaction is that everything is okay, and that this will
copy the first 10 characters of "hello, there" into the buffer
pointed to by s.

however, this is not the case (at least with my compiler -- gnu g++).
what the compiler thinks is that the portion of the for statement

  "int i=0, t=s;"

is a statement which says "declare 2 integers i and t, and initialize
them to the values '0' and 's', respectively."  this also creates an
error to the effect that s is the wrong type (a pointer to char rather
than an int, but that is not relevant here).

it seems strange to me that when i used then `poor' variable declaration
technique of c (define all variables like `i' at the beginning of the
module) that the comma operator was a valuable part of the for statement
syntax, but that conversion to `proper' c++ variable declaration
technique (at the point of usage) invalidates the format of the for
statement.  why is this?

-- 
Kurt Baudendistel [GRA McClellan]
Georgia Tech, School of Electrical Engineering, Atlanta, GA  30332
USENET: ...!{allegra,hplabs,ihnp4,ulysses}!gatech!gt-eedsp!$me
INTERNET: $me@gteedsp.gatech.edu

karl@haddock.ISC.COM (Karl Heuer) (07/20/88)

In article <347@gt-eedsp.UUCP> baud@gt-eedsp.UUCP (Kurt Baudendistel) writes:
>  for ( int i=0, t=s; i < 10; i++ ) ...
>[intending to perform "t=s" as an assignment, but instead finding it to be a
>declaration]

Basically, you've been hit by what could be considered a design flaw in the
syntax of the "for" statement.  In C, where the three components are
expressions, there's no ambiguity, and one can do multiple initializations via
the comma operator.  In C++, the first component is a statement instead of an
expression (note that all expressions are statements, but not vice versa); if,
as in this case, it's a non-expression statement, then the comma operator is
not legal.  Neither is a semicolon (normally the statement-equivalent of the
comma operator), because of the ambiguity this would create.

The appendix to Bjarne's C++ book suggests that
  for ({int i=0; t=s;} i < 10; ++i) ...
should work, but I haven't tested it.  An even kludgier approach is to use
  for (int i=(t=s, 0); i < 10; ++i) ...
which should be a legal use of the comma operator.

Personally, I disrecommend using a declaration as the first component of a for
statement, since it (improperly, in my opinion) extends to the end of the
enclosing block instead of being restricted to the for statement itself.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

pardo@june.cs.washington.edu (David Keppel) (07/21/88)

[ Tried e-mail, but it bounced on 3 different addresses.  *sigh* ]

In article <347@gt-eedsp.UUCP> baud@gteedsp.gatech.edu writes:
>[ ... int i=0, t=s; ...   Why is this not char *t = char* s? ]

Because C++ is backwards compatible with C, and the C declaration
rules require that statement to declare two integers.  Actually, makes
sense.  G++ complains, however, if you write:

    t=s, int i=0;

which is too bad.

  ;-D on  ( top 3 US $ military aid: Israel, Egypt, Turkey )  Pardo

henry@utzoo.uucp (Henry Spencer) (07/23/88)

In article <347@gt-eedsp.UUCP> baud@gt-eedsp.UUCP (Kurt Baudendistel) writes:
>[in C] the comma operator was a valuable part of the for statement
>syntax, but ... conversion to `proper' c++ variable declaration
>technique (at the point of usage) invalidates the format of the for
>statement.  why is this?

What can one say?  Commas in the for statement are a useful trick in C
that simply doesn't generalize to "proper C++".
-- 
Anyone who buys Wisconsin cheese is|  Henry Spencer at U of Toronto Zoology
a traitor to mankind.  --Pournelle |uunet!mnetor!utzoo! henry @zoo.toronto.edu

fst@mcgp1.UUCP (Skip Tavakkolian) (07/24/88)

In article <5335@june.cs.washington.edu>, pardo@june.cs.washington.edu (David Keppel) writes:
> [ Tried e-mail, but it bounced on 3 different addresses.  *sigh* ]
> 
> In article <347@gt-eedsp.UUCP> baud@gteedsp.gatech.edu writes:
> >[ ... int i=0, t=s; ...   Why is this not char *t = char* s? ]
> 
> Because C++ is backwards compatible with C, and the C declaration
  [ deleted ]

At the risk of getting torched, I should say that allowing this type of
variable declaration, without a clean way to declare multiple variables,
makes the language ugly.  And if I understand correctly, the scope of
the variable declared in such a manner is to end of the current block. In
my opinion this is a mis-feature, since C already allows such thing as:

void foo()
	{
	bar();
		{
		int i; char *cp;

		for (i = 0, cp = "THIS"; i < 4; i++) *cp++ = 'z';
		}
	baz();
	}

I am just learning C++ and I know that I (dare I say it) love the language.
It is mostly very clean and logical.  Could someone give me one good reason
for allowing such variable declarations?

Sincerely
-- 
Fariborz ``Skip'' Tavakkolian
UUCP	...!uw-beaver!tikal!mcgp1!fst

UNIX is a registered trademark of AT&T