2

我正在尝试将文件指针数组传递给函数(不确定术语)。谁能解释一下发送“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);
    }
4

2 回答 2

2

尝试:

void openfiles (FILE *in[], FILE **out)
{
    in[0] = fopen("in0", "r");
    in[1] = fopen("in1", "r");
    *out   = fopen("out", "w");
}

并调用它openfiles (in, &out);。此外,“指针数组”是模棱两可的。也许称它为“文件指针数组”?

于 2013-03-01T06:24:34.560 回答
0

你需要pointer to array of FILE* type,像我在下面的函数中那样做。还要添加()括号(*in)覆盖优先级,因为默认情况下[]优先级高于*运算符。请参阅:运算符优先级

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;
}

输出:

g ab

键盘

对于OP:另请阅读 Lundin 对我的回答的评论 很有帮助!

于 2013-03-01T07:11:59.887 回答