C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Array declaration question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
dave.lowe@gmail.com
Guest





PostPosted: Wed Dec 08, 2004 8:20 pm    Post subject: Array declaration question Reply with quote



I've been very use to the requirement of using a constant expression
for array size during array declaration.

I was just told that the C++ standard allows the following:

...
void foo(int n) {
int myArray[n];
for (int i = 0; i < n; myArray[i++] = 0);
}

int main() {
int arraySize = 1000;
foo(arraySize);
return 0;
}

Now, this *seems* to be just plain wrong; however, some c++ compilers
accept it, while some don't. I thought the standard stated you must
use a constant expression.

Can anyone please tell me what the actual standard says is legal
(regardless of how compilers implement it)?

Thanks!

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Alf P. Steinbach
Guest





PostPosted: Wed Dec 08, 2004 8:47 pm    Post subject: Re: Array declaration question Reply with quote



* [email]dave.lowe (AT) gmail (DOT) com[/email]:
Quote:

I was just told that the C++ standard allows the following:

..
void foo(int n) {
int myArray[n];
for (int i = 0; i < n; myArray[i++] = 0);
}

That's incorrect.

It is allowed in C99 (C is not C++, and C99 is not previous
versions of C).


Quote:
Can anyone please tell me what the actual standard says is legal
(regardless of how compilers implement it)?

n must be a compile-time integral constant, except when you allocate
the array dynamically.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Andrew Koenig
Guest





PostPosted: Wed Dec 08, 2004 10:26 pm    Post subject: Re: Array declaration question Reply with quote



<dave.lowe (AT) gmail (DOT) com> wrote


Quote:
I was just told that the C++ standard allows the following:

void foo(int n) {
int myArray[n];
for (int i = 0; i < n; myArray[i++] = 0);
}

It doesn't. Whoever told you that is mistaken.

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.