#include <iostream>
#include <new>
usingnamespace std;
int main (void)
{
int i , n;
int *p;
cout<<"how many numbers would you like to type?"<<endl;
cin>>i;
p = newint [i];
for (n=0 ; n<i ; n++)
{
cout<<"enter a number :";
cin>>p[n];
}
cout<<"you have entered :";
for (n = 0 ; n<i ; n++)
{
cout<<p[n]<<", "<<endl;
}
delete [] p;
return 0;
}
#include <iostream>
#include <new>
usingnamespace std;
int main (void)
{
int i , n;
cout<<"how many numbers would you like to type?"<<endl;
cin>>i;
int p [i];
for (n=0 ; n<i ; n++)
{
cout<<"enter a number :";
cin>>p[n];
}
cout<<"you have entered :";
for (n = 0 ; n<i ; n++)
{
cout<<p[n]<<", "<<endl;
}
return 0;
}
why to use pointers in the first one and it have the same result?
The second example uses a C99 feature that allows you to specify a variable as an array size. It is not available in C++. GNU C++ pulls it in, to make it behave correctly you need to specify -pedantic on the compiler line. https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
...or not error: ISO C++ forbids variable length arrays
This code will compile only for you and those who happens to have this specific extension enabled. It would not compile for anyone else, or even might behave differently for people with another similar extension.
YOu should not use non-standard compiler extensions and should stick to the standard instead.
To do that, enable pedantic warnings (or threat hem as errors) and specify used standard explicitely.