c - 为什么我会在这个 while 循环中遇到段错误?

标签 c segmentation-fault

每当我执行这个程序时,就会抛出一个段错误。该程序很简单:计算给定字符串中元音的数量。这是程序:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

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' <= a && a <= '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 <count>] [-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<argc; i++){
         strsize += strlen(argv[i]);
         if (argc > 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<=(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<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 > (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    
}

GDB 报告段错误发生在 while(in[i] != '\0')。但是,原因使我不知所措。感谢任何见解。

最佳答案

int i; //Dummy index

尚未初始化。 i 具有自动存储类,这意味着它不会被初始化为默认值,因此可以包含任何垃圾值。未初始化的自动变量的值是未定义的。

关于c - 为什么我会在这个 while 循环中遇到段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19174510/

相关文章:

c - 段错误和堆栈溢出有什么区别?

c# - 如何创建一个磁力链接客户端(以前针对ThePiratBay)

c - Pthread 屏障和树莓派

c - 没有注释掉未使用的语句时出现浮点异常?

c - 是否可以将计时器值存储到变量中?

c++ - C++ 中的 Trie 实现

c - 将 memcpy 与 void 指针一起使用时出现段错误 - C

c - getline 中 memcpy 出现奇怪的段错误

c++ - 无法从子类访问 QMainWindow 变量

c - 为什么 "noreturn"函数返回?