简体   繁体   English

将 void** 转换为 int 的二维数组 - C

[英]casting void** to 2D array of int - C

i am trying to cast a void** pointer to an int** 2D array in C我正在尝试将 void** 指针转换为 C 中的 int** 2D 数组

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 dont 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_data 直接转换为 int** 当我将其分配给 graph->graph 时,如下所示:

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谢谢

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:数组形式的大小为 MxN 的矩阵:

+--------------+--------------+-----+----------------+--------------+-----+------------------+
| 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: A 和同一个“矩阵”的指针形式:

+-----------+-----------+-----------+-----+
| 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.如果您分配正确的大小并不重要,这两个变量根本不兼容,这就是您的编译器告诉您的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM