0

I'm very new to C and have difficulty creating simple programs like this. Any help would be appreciated.

C Code

  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 }

Make output

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

~

Community
  • 1
  • 1
Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109
  • At line 21, you do not declare the type of function parameter `pipe_arr`. – John Bollinger Feb 09 '16 at 18:34
  • 1
    Please don't include line-numbers in source, it makes it hard to copy and try ourselves. Instead mark out the problematic lines with a comment. – Some programmer dude Feb 09 '16 at 18:35
  • Your functions are in the wrong order. `pipe_wrapper` should be first, then `create_mapper_pipes`, then `main`. (I'm puzzled why the first warning didn't make this issue obvious to you.) – David Schwartz Feb 09 '16 at 18:35
  • You need to declare functions before using them. Either move the definition of `create_mapper_pipes` up or declare the function before calling it. – fuz Feb 09 '16 at 18:35
  • Additionally, `main()` calls `create_wrapper_pipes()` without an in-scope prototype, and `create_wrapper_pipes()` calls `pipe_wrapper()` without an in-scope prototype. – John Bollinger Feb 09 '16 at 18:36
  • 1
    And you should ***really*** check out [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) for a good beginners book. C is not Java or C# (which you seem to be coming from). – Some programmer dude Feb 09 '16 at 18:36

2 Answers2

3

1) Define the function prototypes before you are calling the functions to get rid of the implicit declaration warnings.

2) int[] pipefd is not a valid C syntax. Use int pipefd[] instead.

3) pipe_arr doesn't have an explicit type.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
  • How do I give an explicit type to pipe_arr, I tried this ` create_mapper_pipes(int pipe_arr[][])` – Daniel Kobe Feb 09 '16 at 18:47
  • new make output http://pastebin.com/z7Zd0F4S and updated C code http://pastebin.com/9tRpdDuJ – Daniel Kobe Feb 09 '16 at 18:50
  • Well, yes. The compiler needs to know the *second* dimension size of the two-dimensional array, as it needs to calculate properly the location in the memory of the indexed member. So just declare it as `int pipe_arr[][2]` – Eugene Sh. Feb 09 '16 at 18:53
2
  1. Declare (or define) functions before you use them! If necessary, use forward declarations.

  2. Look at this:

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

    You need to provide a type for the parameter pipe_arr.

  3. Though it's possible to go without, add a return 0; to the end of main.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Daniel Jour
  • 15,896
  • 2
  • 36
  • 63