Initializing and accessing a pointer from an array of pointers
- by idealistikz
Suppose I have the following:
void **Init(int numElems)
{
//What is the best way to intialize 'ptrElems' to store an array of void *'s?
void **ptrElems = malloc(numElems * sizeof(void *));
return ptrElems;
}
//What is the best way to return a pointer pointing at the index passed as a parameter?
void **GetPtr(void **ptrElems, int index)
{
void **elem = elems + (index * sizeof(void *));
return elem;
}
First, what is the best way to intialize 'ptrElems' to store an array of pointers? I use malloc because assigning it to an array will not persist after the end of the function.
Second, what is the best way to point to the pointer at the specified index? I tried typecasting the first line of the 'GetPtr' function to ensure proper pointer arithmetic, but I receive the warning, 'initialization from incompatible pointer type'. Is it necessary to typecast?