title: Why am I getting a segmentation fault at this while loop?
body: <p>Whenever I execute this program, a segmentation fault is thrown. The program is simple: count the number of vowels in a given string. This is the program:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;assert.h&gt;
  
  int vowelC(char *in, int c){
    //Declarations
    int i; //Dummy index
    char a; //Temp variable
  
    //Validate input
    if(!in || c == 0) return 1;
  
    //Main execution
  
    //Convert to all lower case
    while(in[i] != '\0'){
         a=in[i];
         if('A' &lt;= a &amp;&amp; a &lt;= 'Z')
                a-= ('A' - 'a');
         in[i] = a;
         i++;
    }
  
    //Count the number of vowels
    while(!in[i]){
               if(in[i] == 'a' || in[i] == 'e' || in[i] == 'i' || in[i] == 'o' || in[i] == 'u')
                       c++;
    }
    return 0;                     
   }
  
  void printUsage(void){
     printf("\n\nThis program will count the number of vowels from an entered character string.\n\t[-c &lt;count&gt;] [-h help]\n\n");
  }
  
  int main(int argc, char **argv){
    //Declarations
    int i; //Dummy index
    int j; //Dummy index
    int count = 0;
    int err;
    int strsize = 0;
  
    //Is there at least some user input?
    if(argc == 1){
          printUsage();
          return 1;
  }
  
  //Determine string size if there is more than two user arguments
  for(i=2; i&lt;argc; i++){
           strsize += strlen(argv[i]);
           if (argc &gt; i+1)
              strsize++;
  }
  
  //Declare an array of appropriate length to hold the entered strings
  char *in = (char *)malloc(strsize);
  
  //Validate input
  for(i=1; i&lt;=(argc-2); i++){
  
           //Determine if the user requested usage
           if(strcmp("-h", argv[i])==0){
                           printUsage();
                           return 1;
           }
  
           else if(strcmp("-c", argv[i])==0){
                           //There must be at least one argument after a call 
                           if((i+1) == argc){
                                    printUsage();
                                    return 1;
                           }
                           38
                           //Run another loop to retrieve string inputs
                           for(i=2; i&lt;argc; i++){
                                    for(j=0; j != '\0'; j++){//Determine if a sting is really a string
                                             if(isalpha(argv[i][j])){
                                                                     printUsage();
                                                                     return 1;
                                             }
                            }
                            strcat(in, argv[i]);
                            if(argc &gt; (i+1))
                            strcat(in, " ");
                            }
  
           }
  
           else{//Unknown input
                          printUsage();
                          return -1;
           }
  
  //For bracket
  }
  
  err = vowelC(in, count);
  assert(!err);
  printf("\n\nThe number of vowels counted is %d.\n\nPlease press enter to exit...", count);
  getchar();
  return 0;
  //Main Bracket    
  }
  </code></pre>
  
  <p>GDB reports the segmentation fault occurs at <code>while(in[i] != '\0')</code>. However, the reason eludes me. Any insight is appreciated.</p>