I'm having some serious issues with my introduction to pointers assignment. The assignment is not complete, but I can't get past the return of a pointer in the function createArray. There is something wrong but no errors coming up. It won't even output my getSize function even though it's on the top of my main.
Please help. I'm thinking my problem is I can't create a constant correctly. I don't know if the way I'm doing it by creating a variable with getSize then assigning it to a constant is working correctly.
There are at least two problems with the createArray function.
1. array is a local variable and will no longer exist after the function has ended.
2. The size of an array needs to be a compile-time constant.
The only way you can create an array with a non-fixed size is to use the new keyword.
1 2 3 4
int *createArray(int size)
{
returnnewint[size];
}
When you create something with new it will stay "alive" until you explicitly use delete (If you don't use delete you'll have memory leak) so this will fix the first problem as well.
It's a bit cumbersome having to keep track of pointers, the size, and call delete, etc. so to make it easier I can strongly recommend using std::vector instead of arrays. It might look a bit complicated at first but it's a very useful tool, I promise.