title: Odd behavior of function with variable number of parameters in C
body: <p>I have the following C function with a variable number of arguments, which is supposed to search the <code>char* word</code> through a hashtable and write <code>true</code> or <code>false</code> in a file which, if specified, is the second parameter; otherwise, it is <code>stdout</code>.</p>
  
  <p>It works fine if I specify the name of the file, the problem is when I don't (e.g. <code>find("foo")</code>). In this case it writes the result in a file named <code>foo</code> instead of <code>stdout</code>.</p>
  
  <p>What is the cause?</p>
  
  <pre><code>void find(char* word, ...)
  {
  va_list list;
  char *fname = NULL;
  va_start(list, word);
  FILE* f;
  fname = strdup(va_arg(list, char*));
  va_end(list);
  if (&lt;condition&gt;)    // condition suited for the case in which the file name is received 
      f = fopen(fname, "a");
  else
      f = stdout;
  if (member(word))
      fprintf(f, "True\n");
  else
      fprintf(f, "False\n");
  }
  </code></pre>
  
  <p>In place of <code>&lt;condition&gt;</code> I've tried <code>fname != NULL</code> and <code>strlen(fname) &gt; 0</code> but those don't apply and it keeps seeing <code>fname</code> as <code>word</code> when <code>fname</code> is not specified.</p>
  
  <p>Thank you very much for any help you can provide.</p>