C 程序创建未编译的管道数组



我对 C 语言很陌生,很难创建这样的简单程序。任何帮助将不胜感激。

C 代码

  1 #include <sys/wait.h>
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <unistd.h>
  5 #include <string.h>
  6 #include <time.h>
  7 
  8 const int NUM_OF_MAPPERS = 4;
  9 const int NUM_OF_REDUCERS = 26;
 10 
 11 const int PIPE_READ_END = 0;
 12 const int PIPE_WRITE_END = 1;
 13 const int PIPE_BUFFER_SIZE = 32;
 14 
 15 int main(void) {
 16     // Setup the mapper pipes
 17     int mapper_pipes[NUM_OF_MAPPERS][2];
 18     create_mapper_pipes(mapper_pipes);
 19 }
 20 
 21 void create_mapper_pipes(pipe_arr) {
 22     int i;
 23     for (i = 0; i < NUM_OF_MAPPERS; i++) {
 24         pipe_wrapper(pipe_arr[i]);
 25     }
 26 }
 27 
 28 void pipe_wrapper(int[] pipefd) {
 29     int ret = pipe(pipefd);
 30     if (ret == -1) {
 31         perror("Error. Failed when trying to create pipes.");
 32         exit(EXIT_FAILURE);
 33     }
 34 }

进行输出

cc -c -Wall -Wextra pipe_arr.c
pipe_arr.c: In function 'main':
pipe_arr.c:18:5: warning: implicit declaration of function 'create_mapper_pipes' [-Wimplicit-function-declaration]
pipe_arr.c: At top level:
pipe_arr.c:21:6: warning: conflicting types for 'create_mapper_pipes' [enabled by default]
pipe_arr.c:18:5: note: previous implicit declaration of 'create_mapper_pipes' was here
pipe_arr.c: In function 'create_mapper_pipes':
pipe_arr.c:21:6: warning: type of 'pipe_arr' defaults to 'int' [-Wmissing-parameter-type]
pipe_arr.c:24:9: warning: implicit declaration of function 'pipe_wrapper' [-Wimplicit-function-declaration]
pipe_arr.c:24:30: error: subscripted value is neither array nor pointer nor vector
pipe_arr.c:21:6: warning: parameter 'pipe_arr' set but not used [-Wunused-but-set-parameter]
pipe_arr.c: At top level:
pipe_arr.c:28:25: error: expected ';', ',' or ')' before 'pipefd'
pipe_arr.c: In function 'main':
pipe_arr.c:19:1: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [pipe_arr.o] Error 1

~

1) 在调用函数以摆脱implicit declaration警告之前定义函数原型。

2) int[] pipefd不是有效的 C 语法。请改用int pipefd[]

3) pipe_arr没有显式类型。

  1. 在使用函数之前声明(或定义)函数!如有必要,请使用前向声明

  2. 看看这个:

    void create_mapper_pipes(pipe_arr) {
     // ...
    }
    

    您需要为参数 pipe_arr 提供类型。

  3. 虽然可以不带,但在main末尾添加一个return 0;

最新更新