C:如何将双指针传递给函数

发布于 2024-10-06 02:29:53 字数 412 浏览 2 评论 0原文

当我将双指针传递给函数来初始化内存时,我遇到分段错误

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

临走之时 2024-10-13 02:29:53

就像其他人所说的那样,您需要在 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;
        }
   }
}

并且 main 将是:

int main()
{
    double **A;
    initialize(&A, 10, 10);
}

另外,您发布的代码在传递 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:

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 main will be:

int main()
{
    double **A;
    initialize(&A, 10, 10);
}

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 access A, because the A in main will not have been initialized. Only a copy of it is initialized the way you do it, and that copy is local to the initialize function, so it's lost when you return.

忆梦 2024-10-13 02:29:53

如果要修改指针到指针,则需要传递指针到指针到指针。

void func(double ***data) { *data = malloc(sizeof(double*)*10); for.... };
double ** data; func(&data);

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);
才能让你更想念 2024-10-13 02:29:53

一方面,initialize 中的 AmainA 的副本——所以当你回到 main 时,其A仍未初始化。如果你尝试使用它——繁荣!

要通过引用方式传递给initialize,需要将参数类型改为double***,并传入&A主要。然后,当你在initialize中使用它时,每次都需要取消引用它,即*A

Well for one thing, the A inside initialize is a copy of the A in main -- so when you get back to main, its A is still uninitialized. If you try and use it -- boom!

To pass it to initialize 'by reference', you need to change the parameter type to double*** and pass in &A in main. Then, when you use it in initialize, you need to dereference it each time, i.e. *A.

再见回来 2024-10-13 02:29:53
  1. 您没有检查内存不足错误。失败。

  2. 您将 BY VALUE 未初始化的值 A 传递给initialize(),然后对其进行初始化。但回到 main() 中,局部变量 A 仍未初始化。相反,您可以让initialize()返回double**(例如A =initialize(...))或修改initialize(),使其第一个形式参数为< code>double ***pA ,您使用 *pA = (double**)malloc(...);

    进行初始化

  1. You are not checking for out of memory errors. Fail.

  2. 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 a double ***pA that you initialize with *pA = (double**)malloc(...);

极致的悲 2024-10-13 02:29:53

这是你不想做的事情。不必为此使用 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;
}

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:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文