铸造无效**来为int的二维数组 - Ç [英] casting void** to 2D array of int - C

查看:93
本文介绍了铸造无效**来为int的二维数组 - Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图施放一个无效**指向int **二维数组用C

i am trying to cast a void** pointer to an int** 2D array in C

下面是我试图用劳动code(与所有的外来位去掉):

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;
}

当我编译,它工作正常,但给了我一个警告变量矩阵设置,但没有使用。我真的不希望有使用临时矩阵变量,因为该功能只应该初始化数组,而不是把任何东西它;但是,如果我尝试直接投graph_data为int **当我assiging它graph->图如下所示:

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

感谢

推荐答案

编译器是正确的,数组的数组(或指向数组)是不一样的一个指针的指针。试想想,他们将如何在内存中排列:

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:

大小的M×N的数组的数组形式的矩阵:

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和指针的形式指针相同的矩阵:

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


+-----------+-----------+-----------+-----+
| 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.

这篇关于铸造无效**来为int的二维数组 - Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆