is this a way of creating an array of 3 int called x in C++??
(I've seen this somewhere.)
This is a discussion on int x = new int[3]; within the C++ Programming forums, part of the General Programming Boards category; is this a way of creating an array of 3 int called x in C++?? (I've seen this somewhere.)...
is this a way of creating an array of 3 int called x in C++??
(I've seen this somewhere.)
yes it is
but what's the difference between
andCode:int x[3] = {0};
???Code:int x = new int[3];
> int x = new int[3];
This should be
Code:int *x = new int[3];
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
The former creates an array named x, of 3 ints, and zero initialises them. The latter will not even compile.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
That syntax isn't quite right. It should be:
This is called dynamic allocation. The benefit is you can allocate whatever sized array you need at runtime. The disadvantage is you have to ensure the delete is called when you no longer need the array.Code:int* x = new int[3]; //... delete [] x;
In C++ an alternative to this could be std::vector
Edit: Wow I'm slow...
"Think not but that I know these things; or think
I know them not: not therefore am I short
Of knowing what I ought."
-John Milton, Paradise Regained (1671)
"Work hard and it might happen."
-XSquared
to laser light, why won't it compile??
Read the posts by Salem and JaWiB. You need a pointer to int.to laser light, why won't it compile??
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
okkkk!!! thanks!!
Sorry for the silly that silly post ;P