title: C programming & pointers
body: <p>This code is for converting character numbers to integers number and works good. I can't understand several instances about <strong>ascii_to_int</strong> function code!! The first case is :</p>
  <pre><code>while (*str == ' ') str++;
  if (*str == '-' || *str == '+')
      sign = (*str++ == '-') ? -1 : 1;
  </code></pre>
  <p>I see that <code>while (*str == ' ') str++;</code> is for removing the leading blanks from a string. But I do understand about the third line: why should I use <code>*str++</code>? Why not use <code>str++</code> as two lines before? When should I use '<code>*</code>' asterisk in similar cases? Despite the fact that there may be no leading blanks in the string to get removed, therefore <code>while (*str == ' ') str++;</code> never executes, so why again we can use <code>*str++</code> and not <code>*str</code> instead in the third line? I know that pointers point to a memory location too.
  And second case is:</p>
  <pre><code>while (*str)
          if ((*str &gt;= '0') &amp;&amp; (*str &lt;= '9'))
              *value = (*value * 10) + (*str++-48);
          else {
              printf(&quot;\nwarning :the &lt;%c&gt; is invalid character.\n&quot;,*str);
              break;
          }
  </code></pre>
  <p>Surely we know that first character of string always is a <em>sign</em> ('<code>-</code>' or '<code>+</code>') and that is never between '0' to '9' characters, so always <code>else</code> block must execute. My means is by this part:</p>
  <pre><code>else {
      printf(&quot;\nwarning :the &lt;%c&gt; is invalid character.\n&quot;,*str);
      break;
  }
  </code></pre>
  <p>I am seriously confused!!
  Here's the code:</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  void ascii_to_int(int*, char*);
  int main() {
      int number;
      char s[10];
      printf(&quot;enter numeric string : &quot;);
      gets(s);
      ascii_to_int(&amp;number, s);
      printf(&quot;\nnumeric value is : %d&quot;,number);
      _getch();
      return 0;
  }
  void ascii_to_int(int*value,char*str) {
      int sign = 1;
      *value = 0;
      while (*str == ' ') str++;
      if (*str == '-' || *str == '+')
          sign = (*str++ == '-') ? -1 : 1;
      while (*str)
          if ((*str &gt;= '0') &amp;&amp; (*str &lt;= '9'))
              *value = (*value * 10) + (*str++-48);
          else {
              printf(&quot;\nwarning :the &lt;%c&gt; is invalid character.\n&quot;,*str);
              break;
          }
          *value *= sign;
  }
  </code></pre>