简体   繁体   中英

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

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.

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.

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.

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]...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM