Compile with Visual C++.
At compile time, we may not know how many elements are need to be allocated.
So, we would not know what array size to be defined. If that is the case, how are we going to dynamically allocate the array of pointers?
Printable View
Compile with Visual C++.
At compile time, we may not know how many elements are need to be allocated.
So, we would not know what array size to be defined. If that is the case, how are we going to dynamically allocate the array of pointers?
Why do you need an array? use vectors
Code:int* someData;
//at one point you know how many elements you need
someData = new int[ELEMENTS];
//fill the array
delete [] someData;
There are few if any reasons to use the dynamic array in indigo0086's example over a vector. This does basically the same thing as that. You can also use functions like push_back or resize to make things easier on yourself.Code:vector<int> someData(ELEMENTS);
As you might have guessed, but no one's actually said, the second one shouldn't compile. Unless it's a parameter to a function, in which case both of those code samples are the same. After all, thisQuote:
1 quick question:
andCode:menuItem_t **ptr_1;
Are they the same?Code:menuItem_t *ptr_1[];
is the same as thisCode:int main(int argc, char *argv[])
Code:int main(int argc, char **argv);
There are still few if any good reasons to go this route rather than use a vector, but if you are more comfortable with it (or the other person is making the decision) then the syntax would be:However, using MAXSIZE for the size of the array is something you usually do with static arrays, not dynamic ones. You would use the same code you had above with the 5, but instead of 5 you would use a constant like MAXSIZE.Code:menuItem_t **someData;
someData = new menuItem_t*[MAXSIZE];
// later, when you are done:
delete [] someData;
If MAXSIZE is not constant, then the code I just posted would work. Remember also that the pointers aren't initialized, so you'll have to do that yourself.