title: Parsing optional command line arguments in C
body: <p>I have a program that takes in optional arguments. The necessary arguments are a file and integers (1 or more). The optional arguments are a mix of strings and integers. </p>
  
  <p>So a correct input on the command line could be:</p>
  
  <pre><code>./main trace_file 8 12 # (only necessary arguments)
  
  ./main –n 3000000 –p page.txt trace_file 8 7 4 # (with optional arguments)
  </code></pre>
  
  <p>I need to get the integers after <code>trace_file</code> into an array. I'm having trouble figuring out how to do this when the optional arguments are enabled, because another integer is on the command line. A push in the right direction would be greatly appreciated, because I cannot figure out how to do this.</p>
  
  <p>EDIT:
  so far, all I have for parsing the arguments is this:</p>
  
  <pre><code>for(j=2, k=0; j&lt;argc; j++, k++) {
      shift += atoi(argv[j]);
      shiftArr[k] = 32 - shift;
      bitMaskArr[k] = (int)(pow(2, atoi(argv[j])) - 1) &lt;&lt; (shiftArr[k]);
      entryCnt[k] = (int)pow(2, atoi(argv[j]));
  }
  </code></pre>
  
  <p>But this will only work when no optional arguments are entered. </p>