After compiling my C code with -Wall activated, the following warnings appeared
在编译了激活了-Wall的C代码后,出现了以下警告
left operand of comma operator has no effect
which are related with the the multiple arguments presented in my return
statements. The story is the following: Assume to have a bunch of dynamically allocated 3D arrays (A,B and C) and want to do some manipulation on them. The arrays are defined as pointer to pointer to pointer and allocated using malloc (the standard procedure). The manipulation of them will occur in seperate functions. For some reason I declare the function as a triple pointer and as follow:
这与我返回语句中呈现的多个参数有关。这个故事是这样的:假设有一堆动态分配的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);
}
I know that the arrays are passing into the function as reference so essentially there is no need of returning something from this function. But, can you tell me why someone would declare a function this way. This staff confuses me. Thanks in advance
我知道数组作为引用传递到函数中所以本质上不需要从这个函数中返回什么。但是,你能告诉我为什么有人会这样声明一个函数吗?这个员工混淆我。谢谢提前
2 个解决方案
#1
5
, you can use a return(A, B, C) is not C
struct
to return more than one arguments.
return(A, B, C)不是C,可以使用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;
}
Here is an working example with ***
pointers:
下面是一个使用***指针的工作示例:
#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;
}
#2
0
The code
的代码
***func( double ***A, double ***B, double ***C)
{
do some work here on A, B and C
return(A, B, C);
}
is simply wrong, even though it compiles:
是完全错误的,即使它编译:
-
If the function returns everything via its arguments
A
,B
, andC
, it should not have a return type. I. e., it should be declared to returnvoid
:如果函数通过参数A、B和C返回所有内容,那么它不应该有返回类型。即。,申报为无效:
void func( double ***A, double ***B, double ***C) {
-
The syntax
return(A, B, C)
does not do what you think it does. It does not construct a list, or pass three values toreturn
. Instead, it evaluates the expressionA
, throws its value away, evaluatesB
, throws its value away, evaluatesC
, takes the value ofC
as the value of the expression(A, B, C)
, and finally returns that value from the function. (Google "C comma operator" for more information.)语法返回(A、B、C)并不像您认为的那样。它不构造列表,也不传递三个值返回。相反,它计算表达式A,将其值丢弃,计算B,将其值丢弃,计算C,将C的值作为表达式的值(A, B, C),最后从函数中返回该值。(更多信息请访问谷歌“C逗号运算符”。)
-
If your function is declared as returning
void
(as it probably should be), there is simply no need for areturn
statement.如果您的函数被声明为返回void(可能应该是这样),那么根本不需要返回语句。
#1
5
, you can use a return(A, B, C) is not C
struct
to return more than one arguments.
return(A, B, C)不是C,可以使用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;
}
Here is an working example with ***
pointers:
下面是一个使用***指针的工作示例:
#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;
}
#2
0
The code
的代码
***func( double ***A, double ***B, double ***C)
{
do some work here on A, B and C
return(A, B, C);
}
is simply wrong, even though it compiles:
是完全错误的,即使它编译:
-
If the function returns everything via its arguments
A
,B
, andC
, it should not have a return type. I. e., it should be declared to returnvoid
:如果函数通过参数A、B和C返回所有内容,那么它不应该有返回类型。即。,申报为无效:
void func( double ***A, double ***B, double ***C) {
-
The syntax
return(A, B, C)
does not do what you think it does. It does not construct a list, or pass three values toreturn
. Instead, it evaluates the expressionA
, throws its value away, evaluatesB
, throws its value away, evaluatesC
, takes the value ofC
as the value of the expression(A, B, C)
, and finally returns that value from the function. (Google "C comma operator" for more information.)语法返回(A、B、C)并不像您认为的那样。它不构造列表,也不传递三个值返回。相反,它计算表达式A,将其值丢弃,计算B,将其值丢弃,计算C,将C的值作为表达式的值(A, B, C),最后从函数中返回该值。(更多信息请访问谷歌“C逗号运算符”。)
-
If your function is declared as returning
void
(as it probably should be), there is simply no need for areturn
statement.如果您的函数被声明为返回void(可能应该是这样),那么根本不需要返回语句。