If in my program I am going to read a number n and will require an integer array with n elements, what is to stop using this piece of code rather than using the 'new' operator? Is it just that all variables must be declared at the beginning of the program?
what is to stop using this piece of code rather than using the 'new' operator?
The dimension of the array must be a constant expression. A non-const or const variable that has a value that is unknown until run time cannot be used for the dimension of an array.
The dimension of the array must be a constant expression. A non-const or const variable that has a value that is unknown until run time cannot be used for the dimension of an array.
Thanks very much, that's helpful to know. Is that due to reasons associated with memory allocation?
And code777, thanks for that code. I'm just having a bit of trouble understanding why the output of the 'new' operator has to be a pointer rather than just a variable. I have put together some code (which seems to work), where I've done something like:
1 2 3 4 5 6 7 8
int n, i;
cin>>n;
int *array = newint[n];
for (i=0; i<n; ++i)
{cin>>array[i];}
delete[] array;
But isn't array a pointer, so the code should be something like the following for it to work:
1 2 3 4 5 6 7 8
int n, i;
cin>>n;
int *array = newint[n];
for (i=0; i<n; ++i)
{cin>>array*[i];}
delete[] array;
That is, 'cin' in line 6 should write to the address pointed to by the ith element in array?
Arrays are a lot like pointers. What's created at line 3 is an array, not an array of pointers (new returns a pointer), so there's no need to dereference twice ([ ] acts a lot like a dereference operator, as it seems). In fact, attempting to compile that code will result in an error, whether you put the * before or after array (although the error varies).
Thanks very much for that. I think the key bit of information I was missing is that arrays in C++ are like pointers, which are an entirely new concept to me.
I still can't really understand why pointers are useful. If I need to make a variable accessible to a number of functions I don't see why I can't just use global variables. A computer scientist told me that this gets confusing, but I don't see why keeping track of a number of variables is any more confusing than having to keep track of a load of addresses. Looks like I have a lot more reading to do!
Pointers have their uses for creating "links" between values or for easily allocating a block of memory of varying sizes, but they also become considerably more useful when you start dealing with polymorphism. Trust me, they're useful, just maybe not now. :)
Also, in C++, it's a good coding habit to make sure that everything is in as small a scope as possible (almost, anyway). Global variables can start to be a problem when you start creating very complex programs with lots of variables, and he/she was right; they can get confusing. With passing by reference (as I assume that's what you meant by "accessible to a number of functions"), not only is the scope and confusion problem reduced, but your code is also a lot more reusable, and reusability is, to put it in my words, awesome. :P