title: c language strchr segmentation fault character replace
body: <p>I need to replace every occurence of '&amp;' to ',' in a C string.</p>
  
  <p>I did this and it works</p>
  
  <p>Code 1:</p>
  
  <pre><code>char *val, *querydup;
  .
  .
  .
  val=strchr(querydup,'&amp;');
  while(val != NULL) {
  *val=',';
  val=strchr(querydup,'&amp;');
  }
  </code></pre>
  
  <p>In order to be "elegant" I tried the following, but it leads to seg fault, to the point where even my pointer cursor gets corrupted!. weird, I'm running linux inside a vmware vm.</p>
  
  <p>Code 2:</p>
  
  <pre><code>while(val=strchr(querydup,'&amp;') != NULL) {
  *val=',';
  }
  </code></pre>
  
  <p>So what could be wrong?.. </p>
  
  <p>Do you consider code 1 to be "elegant"?... </p>
  
  <p>Regards.</p>