C:如何在双指针传递给函数 [英] C: How to pass a double pointer to a function
本文介绍了C:如何在双指针传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到一个分段错误,当我通过双指针的函数来初始化内存
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.....
推荐答案
如果你想修改指针的指针,你需要传递一个指针的指针的指针。
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);
这篇关于C:如何在双指针传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文