Dynamic sized array?

D

Daniel T.

I recently came across something like the following:

int main() {
unsigned i;
cin >> i;
int arr;
// use arr
}

Is that valid in C++? I thought it was only valid in C99...
 
K

Kai-Uwe Bux

Daniel said:
I recently came across something like the following:

int main() {
unsigned i;
cin >> i;
int arr;
// use arr
}

Is that valid in C++?


No. However, some compiler accept it as an extension. However, a diagnostic
is required: if the compiler accepts it, you should still get a warning.

I thought it was only valid in C99...

I don't know about C99.


Best

Kai-Uwe Bux
 
M

Martin Steen

Daniel said:
I recently came across something like the following:

int main() {
unsigned i;
cin >> i;
int arr;
// use arr
}

Is that valid in C++? I thought it was only valid in C99...



No, it's not valid. Although some compilers (e.g. g++) may
compile it.

If you want dynamic arrays, better use vector-templates from the STL:

#include <vector>

int main()
{
unsigned i;
cin >> i;
std::vector<int> arr(i);
// use arr
return 0;
}

-Martin
 
S

Sumit Rajan

Daniel said:
I recently came across something like the following:

int main() {
unsigned i;
cin >> i;
int arr;
// use arr
}

Is that valid in C++?


No/Not yet.
I thought it was only valid in C99...

Yes, it is valid in C99.

Regards,
Sumit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top