I have this question and I don't know where the problem is.
Correct the code below:
void AddData(void *data, unsigned int value, int index){
data[index] = value;
}
int main(){
unsigned int array[20];
AddData(array, 10, 5);
return 0;
}
error: subscript of pointer to incomplete type 'void'
- How should I modify the code?
- Why put "void *data" in the function AddData() rather than "int data[20]"?
CodePudding user response:
void*
is a pointer to almost anything, and the compiler just doesn't know what it might point to. It's not possible to dereference a void*
pointer without casting it to its correct pointer type first.
In C , it's very uncommon with void*
pointers, because you can use templates to get the correct and actual type instead:
template<typename T>
void AddData(T *data, unsigned int value, int index){
data[index] = value;
}
Now, when being called, the compiler will deduce the type and use it instead of T
, so in effect the argument-type becomes unsigned int*
(for the example in the question).
With all this said, I recommend you don't use plain arrays, and definitely not pointers.
For arrays, use either std::array
or std::vector
instead:
template<typename T>
void AddData(T& data, unsigned int value, size_t index){
data[index] = value;
}
int main(){
std::array<unsigned int, 20> array;
AddData(array, 10, 5);
}
[Note that I pass the std::array
object by reference and not by value in this example. Also note I changed the type of the index
argument]
CodePudding user response:
data[index]
is the same as *(data index)
and for that to work, the size of the elements must be known. In this case, since data
is pointing at an unsigned int
, you need to cast it to unsigned int*
void AddData(void *data, unsigned int value, int index){
static_cast<unsigned int*>(data)[index] = value;
}
int main(){
unsigned int array[20];
AddData(array, 10, 5);
return 0;
}
How should I modify the code?
I would suggest making it a function template using a std::vector
instead:
#include <vector>
template<class T>
void AddData(std::vector<T>& data, const T& value, std::size_t index){
data[index] = value;
}
int main(){
std::vector<unsigned int> array(20);
AddData(array, 10, 5);
}
Why put
void *data
in the functionAddData()
rather thanint data[20]
?
Presumably it's inherited from C code and they cast the void*
to whatever type the array is holding.
CodePudding user response:
The error message is indicating that you cannot use the []
operator on a void*
pointer, because a void*
pointer does not have a specific type, and therefore the compiler does not know how to interpret the size of the memory block it is pointing to.
To fix this issue, you need to change the type of the data
parameter from void*
to the type of the array that you are passing as an argument. In this case, since you are passing an unsigned int
array, you should change the parameter type to unsigned int*
:
void AddData(unsigned int* data, unsigned int value, int index){
data[index] = value;
}
As for the question of why the parameter is a void*
pointer, it is used for flexibility. Using a void*
pointer allows the function to accept a pointer of any type as an argument, so the same function can be used to modify different types of data. This allows for more flexibility and reusability of the function.