I am trying to cast a void**
pointer to an int**
2D array in C
Here is the code that I am trying to work with (with all the extraneous bits removed):
/*assume that I have a data structure called graph with some
*element "void** graph" in it and some element "int order" */
void initialise_graph_data(graph_t *graph)
{
void **graph_data = NULL;
int (*matrix)[graph->order];
size_t size = (graph->order * graph->order) * sizeof(int);
graph_data = safe_malloc(size); /*safe malloc works fine*/
matrix = (int(*)[graph->order])graph_data;
graph->graph = graph_data;
}
When I compile that, it works fine, but gives me a warning that variable matrix
is set but not used. I don't really want to have to use the interim matrix variable because the function is just supposed to initialise the array, not put anything in it; but if I try to cast graph_data
directly to an int**
when I am assiging it to graph->graph
like so:
graph->graph = (int(*)[graph->order])graph_data;
it gives me an assignment from incompatible pointer type warning.
Am I just not casting it properly? Does anyone have any suggestions as to how I can make it work without the interim "matrix" variable? Or if not, what I can do with that variable so that it doesnt give me the warning that it is set but not used?
Thanks
void **
toint
in C.