使用复合文字访问的数组类型安全函数参数
Array type safety function arguments accessed with compound literal
有一个方法是这样定义的:
void corruption(int (*idata[1]), bool (*flags[3]), bool* (*finfo[2]), int* (*error[1]));
我正在尝试使用复合文字将信息传递给它,同时也尊重类型安全定义,但我对此有疑问。
Argument (*idata[1]) is information which is being checked.
Argument (*flags[3]) are information which suggest the method what to do
Argument *(*finfo[2]) are information which are updated by the algorithm changing the original value
Argument *(*error[1]) are information which are updated by the algorithm changing the original value
我不知道如何通过复合文字将任何内容传递给此函数而不会产生错误或不需要的行为。我想尝试使用复合文字执行此操作的原因是因为我试图避免将信息分配给单独的变量,因为代码中已经存在变量。我试过写这个,当然不行。
int data = 5151;
bool f1_flag = false;
bool f2_flag = false;
bool f3_flag = true;
bool* f1_info = points to some variable;
bool* f2_info = points to some variable;
int error_code = 0;
corruption
(
&(int(*)){ &(int[]){data} },
&(bool(*)){ &(bool[]){f1_flag, f2_flag, f3_flag} },
&(bool []){ &(bool(*)){ &(bool[]){f1_info, f2_info } } },
&(int []) { &(int[]){error} }
);
我收到的关于参数 1 和 2 的编译器警告是:
/* warning: initialization of 'int *' from incompatible pointer type 'int (*)[1] */
/* warning: initialization of '_Bool *' from incompatible pointer type '_Bool (*)[3] */
当然参数 3 和 4 也会产生警告并导致分段错误。
我觉得你对类型不是很了解
void corruption(int *idata[1], bool *flags[3], bool **finfo[2], int **error[1]);
void foo(void)
{
int data = 5151;
bool f1_flag = false;
bool f2_flag = false;
bool f3_flag = true;
bool *f1_info = &f1_flag;
bool *f2_info = &f2_flag;
int error_code = 0;
corruption
(
(int *[]){&data},
(bool *[]){&f1_flag, &f2_flag, &f3_flag},
(bool **[]){&f1_info, &f2_info},
(int **[]){&(int *){&error_code}}
);
}
有一个方法是这样定义的:
void corruption(int (*idata[1]), bool (*flags[3]), bool* (*finfo[2]), int* (*error[1]));
我正在尝试使用复合文字将信息传递给它,同时也尊重类型安全定义,但我对此有疑问。
Argument (*idata[1]) is information which is being checked.
Argument (*flags[3]) are information which suggest the method what to do
Argument *(*finfo[2]) are information which are updated by the algorithm changing the original value
Argument *(*error[1]) are information which are updated by the algorithm changing the original value
我不知道如何通过复合文字将任何内容传递给此函数而不会产生错误或不需要的行为。我想尝试使用复合文字执行此操作的原因是因为我试图避免将信息分配给单独的变量,因为代码中已经存在变量。我试过写这个,当然不行。
int data = 5151;
bool f1_flag = false;
bool f2_flag = false;
bool f3_flag = true;
bool* f1_info = points to some variable;
bool* f2_info = points to some variable;
int error_code = 0;
corruption
(
&(int(*)){ &(int[]){data} },
&(bool(*)){ &(bool[]){f1_flag, f2_flag, f3_flag} },
&(bool []){ &(bool(*)){ &(bool[]){f1_info, f2_info } } },
&(int []) { &(int[]){error} }
);
我收到的关于参数 1 和 2 的编译器警告是:
/* warning: initialization of 'int *' from incompatible pointer type 'int (*)[1] */
/* warning: initialization of '_Bool *' from incompatible pointer type '_Bool (*)[3] */
当然参数 3 和 4 也会产生警告并导致分段错误。
我觉得你对类型不是很了解
void corruption(int *idata[1], bool *flags[3], bool **finfo[2], int **error[1]);
void foo(void)
{
int data = 5151;
bool f1_flag = false;
bool f2_flag = false;
bool f3_flag = true;
bool *f1_info = &f1_flag;
bool *f2_info = &f2_flag;
int error_code = 0;
corruption
(
(int *[]){&data},
(bool *[]){&f1_flag, &f2_flag, &f3_flag},
(bool **[]){&f1_info, &f2_info},
(int **[]){&(int *){&error_code}}
);
}