从虚空铸造**为int [英] casting from void** to int

查看:171
本文介绍了从虚空铸造**为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在无效** 指针动态二维数组,我只是想知道我应该如何投/取消引用值,以便他们可以印?

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

当我尝试编译它,编译器给我的警告:提领无效*指针,并错误:无效的使用作废前pression

When I try and compile it, the compiler gives me the warning: "dereferencing 'void *' pointer", and the error: "invalid use of void expression".

我应该怎么做才能铸就无效** 指针,以便我可以打印从 graph-&GT的元素;图

What should I do to cast the void** pointer so that I can print elements from graph->graph?

编辑:

感谢大家的帮助;我不能让int类型**以图形方式>图,因为它需要持有多张类型的数据,我有麻烦实施的只有一个是整型数组**

thanks for everyone's help; i cant make graph->graph of type int** because it needs to hold mulitple types of data, the only one i am having trouble with implementing is the int** array.

我改变矩阵=(INT *的)graph->图形以graph->图=(无效的*)矩阵,并且工作得很好,我能够打印出数组中的元素,但现在如果我实现一个独立的功能:

i changed matrix = (int*)graph->graph to graph->graph = (void*)matrix and that worked fine, i am able to print elements of the array, however now if i implement a separate function:

void print_foo(graph_t *graph)
{
    int i,j;

    for (i = 0; i < graph->order; i++)
    {
        for(j = 0; j < graph->order; j++)
        {
            printf("%d ", ((int**)graph->graph)[i][j]);
        }
        putchar('\n');
    }
}

它只是给了我一个分段错误,但是如果我运行code的该块原富(graph_t *图),它打印二维数组罚款。

it just gives me a segmentation fault, however if i run that block of code in the original foo(graph_t *graph), it prints the 2D array fine.

可以有人请解释发生了什么graph->图,从而它不会打印,如果我从一个不同的函数调用它

can someone please explain what is happening to graph->graph so that it wont print if i call it from a different function

推荐答案

考虑:

typedef struct graph_t
{
    int order;
    void **graph;
} graph_t;

和假设你分配 graph-&GT;图作为数组为int * 等一系列阵列的 INT ,那么你可以,如果你必须写:

and assuming that you allocate graph->graph as an array of int * and a series of arrays of int, then you can, if you must, write:

#include <stdio.h>
#include <stdlib.h>

typedef struct graph_t
{
    int order;
    void **graph;
} graph_t;

extern void *safe_malloc(size_t n);
extern void foo(graph_t *graph);

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 (int i = 0; i < graph->order; i++)
        matrix[i] = safe_malloc(graph->order * sizeof(int));

    /* storing matrix in the data structure */
    graph->graph = (void **)matrix; // Reverse order of assignment
    // C compiler complains without the cast - the cast is nasty!

    printf("%d\n", ((int **)graph->graph)[2][2]);
}

在code应该检查 graph-&GT;排序&gt; = 3 ,以避免溢出问题

然而,该结构是pretty讨厌的,并在的printf()语句就足以进行必要的铸造你知道为什么它是讨厌的。你会好得多与 INT **图; 中的结构:

However, the structure is pretty nasty, and the necessary casting in the printf() statement is enough to make you realize why it is nasty. You'd be far better off with an int **graph; in the structure:

#include <stdio.h>
#include <stdlib.h>

typedef struct graph_t
{
    int order;
    int **graph;
} graph_t;

extern void *safe_malloc(size_t n);
extern void foo(graph_t *graph);

void foo(graph_t *graph)
{
    int **matrix;

    matrix = safe_malloc(graph->order * sizeof(int*)); 

    for (int i = 0; i < graph->order; i++)
        matrix[i] = safe_malloc(graph->order * sizeof(int));

    graph->graph = matrix;

    printf("%d\n", graph->graph[2][2]);
}

这两种编译没有警告,即使在严格的警告级别。无论是已正式被通过创建一个的main()功能锻炼过测试。您还需要有一个函数巴(graph_t *图)来释放分配的内存,当然。

Both of these compile without warnings, even under stringent warning levels. Neither has formally been tested by creating a main() function to exercise it. You also need to have a function bar(graph_t *graph) to release the allocated memory, of course.

这篇关于从虚空铸造**为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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