cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
Beginner's question: Dynamic array size
Beginner's question: Dynamic array size and dynamic memory
Jan 5, 2012 at 7:33pm UTC
closed account (
z1vC5Di1
)
Hello everybody,
I gained all my knowledge about C++ from this webside's tutorial and I am somewhat still confused why this would not work:
1
2
3
int
x; cin >> x;
int
y[x];
My compiler does not complain. Still the tutorial says that in this case I should use dynamic memory:
1
2
3
int
x; cin >> x;
int
*y =
new
int
[x];
I just wonder: What is wrong with the first lines of code?
Thank you for any explenation! Best, Rafael
Last edited on
Jan 5, 2012 at 7:57pm UTC
Jan 5, 2012 at 7:35pm UTC
Cubbi
(4774)
Your compiler must be using a language extension called "variable length array". It's a feature of the C language that is not present in C++, but some compilers (most notably GCC) permit it as an extension.
If you're using gcc, compile with -pedantic.
Jan 5, 2012 at 7:49pm UTC
bluecoder
(763)
in the first code .. array needs to have the constant size as .
1
2
#define SIZE 50
int
y[SIZE];
Topic archived. No new replies allowed.