Pass an array of function pointers

I am trying to pass an array of file pointer to a function (not sure about the terminology). Can someone explain how to send "to [2]" correctly? Thanks.

#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); } 
+2
source share
2 answers

Try:

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

And name it openfiles (in, &out); . In addition, the "array of pointers" is ambiguous. Perhaps call it a "FILE pointer array"?

+2
source

You need pointer to array of FILE* type , do as I did in the bottom function. Also add () parentheses, such as (*in) , to overwrite priority . By default, [] takes precedence over the * operator. SEE: Operator Priority

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

My example above a line can be useful for understanding the concept:

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

output:

 g ab 

Codepad

For OP : also read Lundin's comments on my answer. Get out of the help!

0
source

Source: https://habr.com/ru/post/1490420/


All Articles