title: Separating an Array by Pipes
body: <p>I've done a lot of work since my last question, so I thought it would be fitting to start a new question to have a clean slate.  If this is the wrong way to do it, please let me know.  (I'm new!)</p>
  
  <p>Here is my code:</p>
  
  <pre><code>int do_command(char **args){
  
      // In the end, this function should take an array of args
      // and separate them into UNIX commands that lie before and after
      // pipes that are entered and put them into an array called
      // arrayOfCommands.
      // For example, if the char array args comes in 
      // with {"ls", "-1", "|", "wc"}, there would be two commands
      // which would be "ls -l" and "wc"
  
      // The following number is in my real program, 
      // but using the contents of args above, there will be two
      // commands ("ls -l" and "wc" separated by a pipe)
      // The number is found dynamically by looking for the number
      // of pipes earlier (in our examples so far, just one) and 
      // simply adding one since there has to be a command on either
      // side of the pipe or an error is thrown.
      const int commands = 2;
  
      // I want to create an array that is 2 wide (int commands)
      // and 5 tall (just a constant number because I don't know
      // how many flags a user will enter after each UNIX command
      //
      //  0 1 
      // 0[][]
      // 1[][]
      // 2[][]
      // 3[][]
      // 4[][]
      //
      // Each column holds a UNIX command (Ex: "ls -s")
      // In this example, "ls" would be stored in arrayOfCommands[0][0];
      // And "-l" would be stored in arrayOfCommands[0][1];
      //
      // If there are more than one command (Ex: "ls -s | wc")
      // They would be stored like this:
      // "ls" in arrayOfCommands[0][0];
      // "-l" in arrayOfCommands[0][1];
      // "wc" in arrayOfCommands[1][0];
  
      char arrayOfCommands[commands][5]; //!!! (Declaration)
  
      //counts which column in arrayOfCommands we are on
      int commandNum = 0;
  
      //Counts which part of a command we are on
      int count = 0;
  
      //Array Counters
      int i = 0;
      int j;
  
      //Go through args until we reach the end
      while (args[i] != NULL){
          //if we find something that's not a pipe...
          if(strcmp(args[i], "|")){
              //put it into the array of commands
              arrayOfCommands[commandNum][count] = args[i]; \\!!! (Copying)
              count++;
          }
          //otherwise if we find a pipe...
          else if (!strcmp(args[i], "|")) {
              //we know we are done with that command
              count = 0;
              commandNum++;
          }
          //Looking at the next value in args
          i++;
      }
  
      //Print the new arrayOfCommands
      int col=0;
      int row=0;
      for (col=0;col&lt;commands;col++){
          printf("Command #%d: ",col);
          for (row=0;row&lt;10;row++){
              printf("%c ",arrayOfCommands[col][row]); \\!!! (Printing)
          }
          printf("\n");
      }
  }
  </code></pre>
  
  <p>I've commented the code like crazy to help everyone understand my code.  To explain further, I am creating a UNIX Shell with C.  My program takes in UNIX commands in the form of user input and put them into an array called "args", separated by spaces.</p>
  
  <p>The code above is working specifically with piping and seeks to further separate the code into commands that fall before and after pipes ("|").  Go ahead and ask questions and I'll do my best to help explain my thought process.</p>
  
  <p>I think I have the coding part somewhat correct, but when I try to print the arrayOfCommands, I get something like:</p>
  
  <p>Command 0: % # ! ##$<br>
  Command 1: *   ^@  @!<br>
  (These are the actual symbols, of course)</p>
  
  <p>I've put "//!!!" behind the lines that I assume are causing the problem.</p>