返回带有多个参数的语句

dim*_*pep 2 c arrays pointers return multiple-arguments

在激活-Wall编译我的C代码后,出现以下警告

left operand of comma operator has no effect
Run Code Online (Sandbox Code Playgroud)

这与我的return陈述中提出的多个论点有关.故事如下:假设有一堆动态分配的3D数组(A,B和C),并希望对它们进行一些操作.数组被定义为指向指针的指针,并使用malloc(标准过程)进行分配.对它们的操纵将在单独的功能中进行.出于某种原因,我将该函数声明为三指针,如下所示:

***func( double ***A, double ***B, double ***C)
{
  do some work here on A, B and C
  return(A, B, C);
}
Run Code Online (Sandbox Code Playgroud)

我知道数组作为引用传递给函数,所以基本上不需要从这个函数返回一些东西.但是,你能告诉我为什么有人会这样宣布一个功能.这个工作人员让我困惑.提前致谢

ser*_*gej 5

return(A, B, C) is not C,您可以使用a struct来返回多个参数.

struct array3d{
  double* A;
  double* B;
  double* C;
};

struct array3d* func(struct array3d* p) {
  /* do some work here on p->A, p->B and p->C */
  return p;
}
Run Code Online (Sandbox Code Playgroud)

这是一个带***指针的工作示例:

#include <stdio.h>
#include <malloc.h>

struct array3d {
  double*** A;
  double*** B;
  double*** C;
};

struct array3d* func(struct array3d* p) {
  /* do some work here on A, B and C */

  ***p->A /= 42.0;
  ***p->B /= 42.0;
  ***p->C /= 42.0;

  return p;
}

int main()
{
    struct array3d arr;
    struct array3d* p_arr;

    double A[] = { 1.0,  3.0}; // ...
    double B[] = {-1.0, -2.0};
    double C[] = { 2.0,  4.0};

    double* p1A = A;
    double* p1B = B;
    double* p1C = C;

    double** p2A = &p1A;
    double** p2B = &p1B;
    double** p2C = &p1C;

    arr.A = &p2A;
    arr.B = &p2B;
    arr.C = &p2C;

    p_arr = func(&arr);

    printf("(A = %f, B = %f, C = %f)\n", ***p_arr->A, ***p_arr->B, ***p_arr->C);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)