9

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

1

1 Answer 1

49

The compiler is right, an array of arrays (or a pointer to an array) is not the same as a pointer to a pointer. Just think about how they would be laid out in memory:

A matrix of size MxN in the form of an array of arrays:

+--------------+--------------+-----+----------------+--------------+-----+------------------+
| matrix[0][0] | matrix[0][1] | ... | matrix[0][N-1] | matrix[1][0] | ... | matrix[M-1][N-1] |
+--------------+--------------+-----+----------------+--------------+-----+------------------+

A and the same "matrix" in the form of pointer to pointer:

+--------+     +-----------+-----------+-----------+-----+
| matrix | --> | matrix[0] | matrix[1] | matrix[2] | ... |
+--------+     +-----------+-----------+-----------+-----+
                 |           |           |
                 |           |           V
                 |           |           +--------------+--------------+-----+
                 |           |           | matrix[2][0] | matrix[2][1] | ... |
                 |           |           +--------------+--------------+-----+
                 |           |
                 |           V
                 |           +--------------+--------------+-----+
                 |           | matrix[1][0] | matrix[1][1] | ... |
                 |           +--------------+--------------+-----+
                 |
                 V
                 +--------------+--------------+-----+
                 | matrix[0][0] | matrix[0][1] | ... |
                 +--------------+--------------+-----+

It doesn't matter if you allocate the correct size, the two variables simply are incompatible which is what your compiler is telling you.

4
  • 3
    Excellent ASCII art. That makes your answer eloquent (and elegant)
    – Rerito
    Aug 27, 2013 at 6:50
  • 1
    nice, is there any ascii tool to do that?
    – artm
    Dec 4, 2015 at 11:58
  • 2
    @artm No idea, I just write these things directly in the text entry box here on the site. :) Dec 4, 2015 at 12:01
  • 2
    impressive for a manual draw, it looks very accurate (all pointers aligned correctly :)
    – artm
    Dec 4, 2015 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.