Topic: Valid or invalid C++ ?
Author: tzs@halcyon.com (Tim Smith)
Date: 1997/10/20 Raw View
Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote:
>>If the compilers are right and I am wrong, how do I initialize my "char *
>>s" in the first part of the "for" loop ?
>
>One way is to move the declaration of `i' to before the for loop:
>
> char * st = "Test";
> char * s;
> int i;
> for (i = 0, s = st; i < 2; ++i)
It's probably better to move the "s = st" out of the loop:
char * st = "Test";
char * s = st;
for (int i = 0; i < 2; ++i )
The advantages are:
(1) Everything is intialized right away (good style, I'm told),
(2) When the compilers catch up with the proposed standard, i will be
local to the loop, which is probably what the original poster wanted.
--Tim Smith
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Edward Diener <eddielee@abraxis.com>
Date: 1997/10/14 Raw View
void function()
{
char * st = "Test";
char * s;
for (int i = 0,s = st; i < 2; ++i)
{
}
}
All my C++ compilers on Windows NT, Borland C++ 5.02, Borland C++
Builder 1.0, Microsoft C++ 5.0, Symantec C++ 7.5, and Watcom C++ 11.0,
give an error message when compiling this code, all on the line of the
"for" loop. Am I really saying here that the variable "s" is of type
integer, as all the compilers claim, or am I just trying to initialize
my variable "s", which is a char * to another char *, as I believe. If
the compilers are right and I am wrong, how do I initialize my "char *
s" in the first part of the "for" loop ? What does the standard say
about this ? Have I found an undefined area of the standard here ?
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/10/14 Raw View
Edward Diener <eddielee@abraxis.com> writes:
> char * st = "Test";
> char * s;
>
> for (int i = 0,s = st; i < 2; ++i)
...
>All my C++ compilers on Windows NT, Borland C++ 5.02, Borland C++
>Builder 1.0, Microsoft C++ 5.0, Symantec C++ 7.5, and Watcom C++ 11.0,
>give an error message when compiling this code, all on the line of the
>"for" loop. Am I really saying here that the variable "s" is of type
>integer, as all the compilers claim, or am I just trying to initialize
>my variable "s", which is a char * to another char *, as I believe.
The compilers are right.
>If the compilers are right and I am wrong, how do I initialize my "char *
>s" in the first part of the "for" loop ?
One way is to move the declaration of `i' to before the for loop:
char * st = "Test";
char * s;
int i;
for (i = 0, s = st; i < 2; ++i)
>What does the standard say about this ?
The grammar for a `for' statement says that the first part is either
an expression-statement or a simple-declaration.
`int i = 0, s = st' is not an expression-statement, so it must be
a simple-declaration. When you follow the details, you'll see that
it declares `s' to be an int.
>Have I found an undefined area of the standard here ?
No.
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/10/14 Raw View
Edward Diener wrote:
>
> void function()
>
> {
>
> char * st = "Test";
> char * s;
>
> for (int i = 0,s = st; i < 2; ++i)
> {
> }
>
> }
>
> All my C++ compilers on Windows NT, Borland C++ 5.02, Borland C++
> Builder 1.0, Microsoft C++ 5.0, Symantec C++ 7.5, and Watcom C++ 11.0,
> give an error message when compiling this code, all on the line of the
> "for" loop.
I think you should write:
const char* st = "Test";
const char* s;
for (int i = (s = st, 0); i < 2; ++i);
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Emmanuel Renieris <er@cs.brown.edu>
Date: 1997/10/15 Raw View
Edward Diener wrote:
>
> void function()
>
> {
>
> char * st = "Test";
> char * s;
>
> for (int i = 0,s = st; i < 2; ++i)
> {
> }
>
> }
>
> All my C++ compilers on Windows NT, Borland C++ 5.02, Borland C++
> Builder 1.0, Microsoft C++ 5.0, Symantec C++ 7.5, and Watcom C++ 11.0,
> give an error message when compiling this code, all on the line of the
> "for" loop. Am I really saying here that the variable "s" is of type
> integer, as all the compilers claim, or am I just trying to initialize
> my variable "s", which is a char * to another char *, as I believe. If
> the compilers are right and I am wrong, how do I initialize my "char *
> s" in the first part of the "for" loop ? What does the standard say
> about this ? Have I found an undefined area of the standard here ?
The compilers are right...
The declaration/initialization
int i = 0, s = st;
(say, at top level) declares two integers and initializes the
second. The comma here separates the two names that are introduced,
and the '=' initializes s.
We are used to writing
for (i=0,s=st;i<2;i++)
but 'i=0,s=st' is just one statement.
(Actually one expression, consisting of two subexpressions connected
by the *comma operator*, and those two expressions happen to be
assignments).
In other words, the first part of the for (the for-init-statement
in the draft standard grammar) is just a C++ statement, so if
you can write it at top level, you can put it there.
I don't think there is a way to get a declaration and an assignment
in just a single statement, (which is what you are looking for), so
I would put one of them outside the for.
(You can always put the for in a separate scope if you want to.)
Hope this helps.
Manos
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/10/15 Raw View
Valentin Bonnard <bonnardv@pratique.fr> writes:
|> Edward Diener wrote:
|> >
|> > void function()
|> >
|> > {
|> >
|> > char * st = "Test";
|> > char * s;
|> >
|> > for (int i = 0,s = st; i < 2; ++i)
|> > {
|> > }
|> >
|> > }
|> >
|> > All my C++ compilers on Windows NT, Borland C++ 5.02, Borland C++
|> > Builder 1.0, Microsoft C++ 5.0, Symantec C++ 7.5, and Watcom C++ 11.0,
|> > give an error message when compiling this code, all on the line of the
|> > "for" loop.
|>
|> I think you should write:
|>
|> const char* st = "Test";
|> const char* s;
|>
|> for (int i = (s = st, 0); i < 2; ++i);
I was actually going to propose the same thing, but with a smiley. You
don't really want people to write code like that, do you?
And while I'm at it, I suppose that the semi-colon on the same line as
the for is a typo. Or... let's indent the following line as well --
keep the maintainance programmer guessing, that's my motto.
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
I'm looking for a job -- Je recherche du travail
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "John Calcote" <jcalcote@novell.com>
Date: 1997/10/16 Raw View
Edward Diener wrote in message <3442B366.CCA53D4@abraxis.com>...
> char * st = "Test";
> char * s;
>
> for (int i = 0,s = st; i < 2; ++i)
>All my C++ compilers on Windows NT, Borland C++ 5.02, Borland C++
>Builder 1.0, Microsoft C++ 5.0, Symantec C++ 7.5, and Watcom C++ 11.0,
>give an error message when compiling this code, all on the line of the
>"for" loop. Am I really saying here that the variable "s" is of type
>integer, as all the compilers claim, or am I just trying to initialize
Unfortunately, you've discovered a sort of precedence issue that the
standard doesn't do anything about. You are allowed to write this code,
right?
int i = 0, s = 4;
Certainly, you would not expect your C++ compiler to instantiate 'i' as an
int, then to treat 's' as if it already existed somewhere else, would you?
What if you wrote this:
int s;
int i = 0, s = 4;
Wouldn't you expect your compiler to tell you on the second line that 's' is
already defined elsewhere? If so, then why does an error here surprise you?
int s;
for (int i = 0, s = 4; ... )
The fact is, when you start a statement with a type and a previously unknown
identifier like this, you've just committed yourself to a declaration. You
must then follow the rules of a declaration, as opposed to a statement.
John Calcote
Directory Services
Novell, Inc.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]