C语言 C:如何将双指针传递给函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4339201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: How to pass a double pointer to a function
提问by veda
I am getting an segmentation fault when I pass the double pointers to the function to initialize the memory
当我将双指针传递给函数以初始化内存时出现分段错误
int main()
{
double **A;
initialize(A, 10, 10);
......
}
void initialize(double **A, int r, int c)
{
A = (double **)malloc(sizeof(double *)*r);
for(int i = 0; i< r; i++) {
A[i] = (double *)malloc(sizeof(double) *c);
for(int j = 0; j < c; j++) {
A[i][j] = 0.0;
}
}
}
How can I pass the double pointers to the functions.....
如何将双指针传递给函数.....
回答by ?imon Tóth
If you want to modify a pointer to pointer you need to pass a pointer to pointer to pointer.
如果要修改指向指针的指针,则需要将指向指针的指针传递给指针。
void func(double ***data) { *data = malloc(sizeof(double*)*10); for.... };
double ** data; func(&data);
回答by IVlad
Like others have said, you need to take a pointer to pointer to pointer in your init function. This is how the initializefunction changes:
就像其他人所说的那样,您需要在 init 函数中使用指向指针的指针。这是initialize函数的变化方式:
void initialize(double ***A, int r, int c)
{
*A = (double **)malloc(sizeof(double *)*r);
for(int i = 0; i< r; i++) {
(*A)[i] = (double *)malloc(sizeof(double) *c);
for(int j = 0; j < c; j++) {
(*A)[i][j] = 0.0;
}
}
}
And mainwill be:
并且main将是:
int main()
{
double **A;
initialize(&A, 10, 10);
}
Also, the code as you posted it should cause no segmentation fault when passing the Apointer in. The segmentation fault most likely occurs when you return from the function and try to access A, because the Ain mainwill not have been initialized. Only a copy of it is initialized the way you do it, and that copy is local to the initializefunction, so it's lost when you return.
此外,您发布的代码在传入A指针时应该不会导致分段错误。当您从函数返回并尝试访问时A,最有可能发生分段错误,因为Ainmain不会被初始化。只有它的一个副本按照您的方式进行初始化,并且该副本是initialize函数的本地副本,因此在您返回时它会丢失。
回答by arsenm
This is the kind of thing you do not want to do. Instead of unnecessarily using an out argument for this, allocate in the function and return the result. Do this instead:
这是你不想做的事情。与其不必要地为此使用 out 参数,不如在函数中分配并返回结果。改为这样做:
int main()
{
double **A;
A = initialize(A10, 10);
}
double** initialize(int r, int c)
{
double **A;
A = malloc(sizeof(double *)*r);
for(int i = 0; i< r; i++) {
A[i] = (double *)malloc(sizeof(double) *c);
for(int j = 0; j < c; j++) {
A[i][j] = 0.0;
}
}
return A;
}
回答by Stuart Golodetz
Well for one thing, the Ainside initialize is a copy of the Ain main-- so when you get back to main, its Ais still uninitialized. If you try and use it -- boom!
一方面,A内部 initialize 是Ain的副本main——所以当你回到 时main,它A仍然是未初始化的。如果您尝试使用它 - 繁荣!
To pass it to initialize'by reference', you need to change the parameter type to double***and pass in &Ain main. Then, when you use it in initialize, you need to dereference it each time, i.e. *A.
将它传递给initialize“引用”,你需要的参数类型更改为double***和传递&A的main。然后,在 中使用时initialize,每次都需要解引用,即*A.
回答by Jan Gray
You are not checking for out of memory errors. Fail.
You pass BY VALUE an uninitialized value A to initialize() and then initialize that. But back in main(), that local variable A is still uninitialized. Instead you might have initialize() return the
double**(e.g.A = initialize(...)) or modify initialize() so its first formal parameter is adouble ***pAthat you initialize with*pA = (double**)malloc(...);
您没有检查内存不足错误。失败。
您将 BY VALUE 一个未初始化的值 A 传递给 initialize() ,然后对其进行初始化。但是回到 main() 中,局部变量 A 仍未初始化。相反,您可能让 initialize() 返回
double**(egA = initialize(...)) 或修改 initialize() 所以它的第一个形式参数是double ***pA您初始化的a*pA = (double**)malloc(...);

