title: optarg from getopt() is always null
body: <p>I'm trying to process an argument parameter using the optarg parameter of getopt(), but it remains null. Could this be something to do with the c99 standard? I know I will need to actually copy the string from optarg but it never even gets set.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;getopt.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char *argv[]) {
      char *optarg;
      int ch; 
  
      char *indir = NULL;
  
      while ((ch = getopt(argc, argv, "d:")) != -1) {
          switch(ch) {
              case 'd':
                  indir = optarg;
                  fprintf(stderr, "Optarg: %s\n", optarg);
                  fprintf(stderr, "Dir name: %s\n", indir);
                  break;
              default : 
                  fprintf(stderr, "Usage:  test -d &lt;input directory&gt;\n");
                  exit(1);
          }
      }
      if(indir == NULL){
          fprintf(stderr, "Input directory required.\n");
          exit(1);
      }
      else{
          printf("Input dir: %s\n", indir);
      }
  
  
      return 0;
  }
  </code></pre>