0

I want to ask user enter an integer to create a 2D array like this:

void* myArray_1[2][userInput];

which is not allowed by compiler because the userInput is not constant.

Then I tried this:

void** myArray_2 = new void*[rowCount];
for(int i = 0; i < rowCount; ++i)
    myArray_2[i] = new void[colCount];

which causes the below error:

Error: array of void is NOT allowed.

How can I create an array of void whose size will be specified by the user input from command line?

Edit:
my function prototype is:
errorType EventGet ( EVENT_HANDLE hEvent, void * pBuffer);

I used this:

void* myArray_3[2][10];
...
myArray_3[0][count++] = myOtherArray[0]; //myOtherArray[0] contains a handle and myOtherArray is an 1D array with size of 2.
EventGet ( myEventHandle, myArray_3[0][i]);

and I got correct result. Now the only thing I want is to allow the user to specify the second dimension size instead of 10.

IndustProg
  • 627
  • 1
  • 13
  • 33
  • 2
    What do you expect to put in an array of void? The error message is pretty clear that this kind of thing isn't allowed. The dynamic size doesn't even matter. – Code-Apprentice Dec 31 '16 at 16:41
  • 2
    `std::vector> myVector(2, std::vector(userInput));` – Igor Tandetnik Dec 31 '16 at 16:41
  • Perhaps you want a 2D array of `void*` instead: `void*** myArray ...`? – Code-Apprentice Dec 31 '16 at 16:42
  • @Code-Apprentice I want to pass it to a function which accepts a 2D array of void. Yes, I want a 2D array of `void*`. – IndustProg Dec 31 '16 at 16:44
  • "a function which accepts a 2D array of void" Please show the signature of the function. I doubt you understand it correctly since an array of void is not allowed. Are you sure it isn't a 2d array of `void*`? – Code-Apprentice Dec 31 '16 at 16:45
  • @Code-Apprentice don't be confused. [A dynamic 2D array is basically an array of pointers to arrays.](http://stackoverflow.com/a/936702/4439444) – IndustProg Dec 31 '16 at 16:52
  • 1
    @GmtK: You are the one who is confused. No function can accept a 2D array of void. (At least, not in C++). Show us the *actual* signature of the function. – Martin Bonner supports Monica Dec 31 '16 at 16:55
  • @GmtK Yes, you can have an array of `void*`. This is not the same as a 2D array of void however, since you cannot allocate an array of void to each `void*`. – Code-Apprentice Dec 31 '16 at 17:01
  • @ I am poor in English. Sorry for it. I meant I pass that 2D array of void to my function. My function has the `void *` type argument. I will edit my question soon. – IndustProg Dec 31 '16 at 17:05
  • Possible duplicate of [What is void\* and to what variables/objects it can point to](http://stackoverflow.com/questions/5104640/what-is-void-and-to-what-variables-objects-it-can-point-to) – Code-Apprentice Dec 31 '16 at 17:39
  • `void*` is NOT a 2D array. It is a pointer to...something...which may be the first element of an array or a single value. Note that if it points to an array, the array must hold elements of some legal type, such as `int`. – Code-Apprentice Dec 31 '16 at 17:40
  • [This](http://stackoverflow.com/questions/11626786/what-does-void-mean-and-how-to-use-it) is probably a better link than the previous one. – Code-Apprentice Dec 31 '16 at 17:41

3 Answers3

4

The kill point is not the passing, the pointing, or any of its ilk, the problem is here:

myArray_2[i] = new void[colCount];

new void[colCount]; says "Make me an array of void." void is a placeholder for "no type" and cannot be instantiated. You effectively would be creating an array of nothing. Nothing has no definition. No size. This cannot be done.

However, OP wishes to duplicate the Variable Length Array

void* myArray_1[2][userInput];

To do this, OP needs to indirect one level further, and store a 2 dimensional array (an array of arrays, really) of void *.

void*** myArray_2 = new void**[rowCount];
for(int i = 0; i < rowCount; ++i)
    myArray_2[i] = new void*[colCount];

This will give the appearance of void* myArray_1[2][userInput];

The preferred C++ approach is to use a container to manage the memory for you and use a std::vector:

std::vector<std::vector<void*>> myVector_1(2, std::vector<void*>(userInput));

Defines a vector that contains a vector that contains pointers to void. The outer vector is presized to 2 elements and the inner vector is presized to userInput elements.

user4581301
  • 33,082
  • 7
  • 33
  • 54
2

Apparently you need an array of void*. This is not the same as a 2D array of void since you cannot allocate an array of void as the error states. The first level is correct:

void** myArray_2 = new void*[rowCount];

However, the second level must be allocated with a valid type such as

void** myArray_2 = new void*[rowCount];
for(int i = 0; i < rowCount; ++i)
    myArray_2[i] = new int[colCount];

Of course, you can put whatever type you wish other than int. You can also mix types if you want.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

Based off what Igor Tandetnik has commented, and given that you know the outer size is constant 2, you could do something like:

std::vector<void*> myArray[2] = {
  std::vector<void*>(userInput), std::vector<void*>(userInput)
};

for (int i = 0; i < userInput; ++i) {
  work on myArray[0][i]...
  work on myArray[1][i]...
}
Edy
  • 462
  • 3
  • 9