casting from void** to int - C
i have a dynamic 2D array stored in a void** pointer, and i am just
wondering how i am supposed to cast/dereference the values so that they
can be printed?
here is an example of what i am trying to do:
\*assume that i have a data structure called graph with some
*element "void** graph" in it and some element "int order" */
void foo(graph_t *graph)
{
int **matrix;
/*safe malloc works fine, it uses calloc to initialise it all to zeroes*/
matrix = safe_malloc(graph->order * sizeof(int*));
for (i = 0; i < graph->order; i++)
matrix[i] = safe_malloc(graph->order * sizeof(int));
/* storing matrix in the data structure */
matrix = (int**)graph->graph;
printf("%d\n", (int)graph->graph[2][2]);
}
when i try and compile it, the compiler gives me the warning:
"dereferencing 'void *' pointer, and the error: invalid use of void
expression.
what should i do to cast the void** pointer so that i can print elements
from graph->graph?
No comments:
Post a Comment