我正在尝试将文件指针数组传递给函数(不确定术语)。有人可以解释发送'in [2]'的正确方法吗?谢谢。
#include<stdio.h>
#include<stdlib.h>
void openfiles (FILE **in[], FILE **out)
{
*in[0] = fopen("in0", "r");
*in[1] = fopen("in1", "r");
*out = fopen("out", "w");
}
void main()
{
FILE *in[2], *out;
openfiles (&in, &out);
fprintf(out, "Testing...");
exit(0);
}
答案 0 :(得分:2)
尝试:
void openfiles (FILE *in[], FILE **out)
{
in[0] = fopen("in0", "r");
in[1] = fopen("in1", "r");
*out = fopen("out", "w");
}
并将其称为openfiles (in, &out);
。而且,“指针数组”是不明确的。也许称它为“FILE指针数组”?
答案 1 :(得分:0)
你需要pointer to array of FILE* type
,就像我在下面的功能中那样做。另外,将()
括号添加到覆盖优先级,因为默认情况下(*in)
优先于[]
运算符。见:Operator Precedence
*
我对字符串的例子对理解这个概念很有用:
void openfiles (FILE* (*in)[2], FILE **out){
(*in)[0] = fopen("in0", "r");
(*in)[1] = fopen("in1", "r");
*out = fopen("out", "w");
}
输出:
#include<stdio.h>
void f(char* (*s)[2]){
printf("%s %s\n", (*s)[0],(*s)[1]);
}
int main(){
char* s[2];
s[0] = "g";
s[1] = "ab";
f(&s);
return 1;
}
对于 OP :还请阅读Lundin对我的回答的评论退出有用!