传递指针数组功能 [英] Passing pointers to arrays to functions
问题描述
我存储指针结构数组我的信息。换句话说,阵列的每个元素是一个指向一个链表。
I am storing my information in an array of pointers to structs. In other words, each element of the array is a pointer to a linked list.
我不知道数组应该有多长等等,而不是初始化数组中的我的main()函数,而不是我的intialize双指针
I don't know how long the array should be, so instead of initializing the array in my main() function, I instead intialize the double pointer
struct graph** graph_array;
然后,一旦我获得该阵列的长度,我尝试使用函数GraphInitialize初始化graph_array的每个元素:
Then once I obtain the length of the array, I try to initialize each element of graph_array using a function GraphInitialize:
int GraphInitialize(struct graph* *graph_array,int vertices)
{
struct graph* graph_array2[vertices+1];
graph_array = graph_array2;
int i;
for (i=0;i<vertices+1;i++)
{
graph_array[i] = NULL;
}
return 0;
}
但出于某种原因,这是不返回更新graph_array主()。基本上,这个功能被本地更新graph_array,并没有改变正在取得。因此,任何时候,我尝试访问的graph_array它赛格故障,因为它未初始化的元素。我在做什么错了?
But for some reason this is not returning the updated graph_array to main(). Basically, this function is updating graph_array locally, and no change is being made. As a result, any time I try to access an element of graph_array it seg faults because it is not initialized. What am I doing wrong?
编辑:在与汤姆的啊康沃我要补充别的东西,使这个更加混乱
Following the convo with Tom Ahh I should add something else that makes this more confusing.
我不直接从主调用GraphIntialize()。相反,我打电话从主要的getData(),并传递一个指向graph_array到GETDATA如下图所示。
I don't call GraphIntialize directly from main(). Instead, I call getdata() from main, and pass a pointer to graph_array to getdata as shown below.
getdata(argc, argv, vertpt, edgept, &graph_array)
int getdata(int argc, char *argv[], int *verts, int *edges, struct graph* **graph_array)
接着的getData会从我的输入文件的顶点的数目,并使用该调用GraphInitialize:
Then getdata gets the number of vertices from my input file, and uses that to call GraphInitialize:
if ((GraphInitialize(&graph_array, *verts)) == -1)
{
printf("GraphCreate failed");
return 0;
}
这导致错误:预计'结构图3ASTERISKS',但参数的类型为结构图4ASTERISKS
This results in an error: "expected 'struct graph 3ASTERISKS' but argument is of type 'struct graph 4ASTERISKS'.
推荐答案
在您指定的东西 graph_array
,您只需将其分配给它的本地副本。在功能它所做的更改不会被看到,可以通过调用者。您需要通过指针的值传递它能够改变它的值。你的函数原型更改为 INT GraphInitialize(图结构*** graph_array,INT顶点)
键,当你调用它,使用 GraphInitialize(安培; graph_array, 42)
。
When you assign something to graph_array
, you simply assign it to its local copy. The changes made to it in the function will not be see-able by the caller. You need to pass it by pointer value to be able to change its value. Change your function prototype to int GraphInitialize(struct graph ***graph_array,int vertices)
and when you call it, use GraphInitialize(&graph_array, 42)
.
当您创建 graph_array2
在code第二个问题是,您声明它是您的本地 GraphInitialize()
的功能。因此,退出你的函数时,graph_array2被破坏,即使你把它分配给 * graph_array
。 (星解引用指针将其分配到它所指向的值)。
Second problem in your code is when you create graph_array2
, you declare it to be local to your GraphInitialize()
function. Thus, when exiting your function, graph_array2 is destroyed, even if you assigned it to *graph_array
. (the star dereferences the pointer to assign it to the value it points to).
更改为分配 * graph_array =的malloc(sizeof的(* graph_array)*顶点);
,你应该罚款
change your assignation to *graph_array = malloc(sizeof(*graph_array) * vertices);
and you should be fine.
这篇关于传递指针数组功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!