C:如何将双指针传递给函数
当我将双指针传递给函数来初始化内存时,我遇到分段错误
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;
}
}
}
如何将双指针传递给函数......
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.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
就像其他人所说的那样,您需要在 init 函数中使用一个指向指针的指针。这就是
initialize
函数的变化方式:并且
main
将是:另外,您发布的代码在传递
A
指针输入。当您从函数返回并尝试访问A
时,最有可能发生分段错误,因为main
中的A
不会已初始化。只有它的一个副本按照您的方式进行初始化,并且该副本是initialize
函数的本地副本,因此当您返回时它会丢失。Like others have said, you need to take a pointer to pointer to pointer in your init function. This is how the
initialize
function changes:And
main
will be:Also, the code as you posted it should cause no segmentation fault when passing the
A
pointer in. The segmentation fault most likely occurs when you return from the function and try to accessA
, because theA
inmain
will not have been initialized. Only a copy of it is initialized the way you do it, and that copy is local to theinitialize
function, so it's lost when you return.如果要修改指针到指针,则需要传递指针到指针到指针。
If you want to modify a pointer to pointer you need to pass a pointer to pointer to pointer.
一方面,initialize 中的
A
是main
中A
的副本——所以当你回到main 时
,其A
仍未初始化。如果你尝试使用它——繁荣!要通过引用方式传递给
initialize
,需要将参数类型改为double***
,并传入&A
中主要
。然后,当你在initialize
中使用它时,每次都需要取消引用它,即*A
。Well for one thing, the
A
inside initialize is a copy of theA
inmain
-- so when you get back tomain
, itsA
is still uninitialized. If you try and use it -- boom!To pass it to
initialize
'by reference', you need to change the parameter type todouble***
and pass in&A
inmain
. Then, when you use it ininitialize
, you need to dereference it each time, i.e.*A
.您没有检查内存不足错误。失败。
您将 BY VALUE 未初始化的值 A 传递给initialize(),然后对其进行初始化。但回到 main() 中,局部变量 A 仍未初始化。相反,您可以让initialize()返回
double**
(例如A =initialize(...)
)或修改initialize(),使其第一个形式参数为< code>double ***pA ,您使用*pA = (double**)malloc(...);
进行初始化
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 ***pA
that you initialize with*pA = (double**)malloc(...);
这是你不想做的事情。不必为此使用 out 参数,而是在函数中分配并返回结果。改为这样做:
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: