C 从空**到整型铸造
我在C 从空**到整型铸造,c,pointers,casting,void-pointers,C,Pointers,Casting,Void Pointers,我在void**指针中存储了一个动态2D数组,我只是想知道如何强制转换/取消引用这些值,以便打印它们 下面是我正在尝试做的一个例子: /* 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; /*saf
void**
指针中存储了一个动态2D数组,我只是想知道如何强制转换/取消引用这些值,以便打印它们
下面是我正在尝试做的一个例子:
/* 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]);
}
它只是给了我一个分段错误,但是如果我在原始foo(graph_t*graph)中运行该代码块,它会很好地打印2D数组
有人能解释一下graph->graph发生了什么,这样当我从不同的函数调用它时,它就不会打印了吗
typedef struct graph_t
{
int order;
void **graph;
} graph_t;
假设您将graph->graph
分配为int*
数组和一系列int
数组,那么如果必须,您可以编写:
#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]);
}
即使在严格的警告级别下,这两种方法也不会发出警告。这两种方法都没有通过创建
main()
函数来进行正式测试。当然,您还需要一个函数bar(graph\u t*graph)
来释放分配的内存。注意:这一行正在泄漏内存:matrix=(int**)graph->graph代码>。您刚刚分配了matrix
返回malloc()
调用。第二,什么是图形?它看起来有一个成员(graph
)是void**
,您不能取消引用void*
(这是您的第二个索引所做的)。那里没有“类型”,因此也没有了解它的机制。没有理由为你的“隐藏”数组设置一个空**。一个void*
将起作用,但在这之前你需要更多地理解指针。另请参阅。非常感谢你的帮助。我必须将图形保持为void**指针,以便能够在该空间中存储多种类型的数据。根据您的回答,我有一个后续问题,但我可能只是编辑我的原始问题,以便使用代码。。
#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]);
}
#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]);
}