Reputation: 105
The problem is as follows. I have a void pointer and I want to allocate string array with that. Is it possible to cast void* to char** like this:
void* ptr = (char**)calloc(size, sizeof(char*))
and then allocate each row of that table? I'm currently running out of ideas.
Upvotes: 0
Views: 188
Reputation: 390
Psuedo Code that should get you what you need.
char **ptr = NULL;
// Allocates an array of pointers
ptr = malloc(sizeof(char *) * (NUM_OF_STRINGS_IN_ARRAY));
If (ptr == NULL)
return; // Do error handling here
for (int i =0; i < NUM_OF_STRINGS_IN_ARRAY; i++)
{
// Allocates each string in the array.
ptr[i] = malloc(strlen(STRING));
if (ptr[i] == NULL)
{
return; // Do error handling here
}
}
Upvotes: 1