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

时间:2013-10-04 06:13:31

标签: 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')发生分段错误。但是,原因让我不知所措。任何见解都表示赞赏。

5 个答案:

答案 0 :(得分:3)

int i; //Dummy index

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

答案 1 :(得分:3)

每个人都已经确定了问题,但没有人回答了这个问题,所以我会把它放在一起:发生段错误是因为你的进程试图访问它不拥有的内存,这是因为你没有' t初始化i的值。

从广义上讲,数组是作为连续内存处理的。您可以将数组元素语法视为一种类型转换的函数,并在从元素类型和索引参数的大小计算的地址处返回内存的内容。在您的情况下,类型为char,sizeof(char)返回1,因此in[i]存储在in[0]i的地址。

索引中的一个疯狂值导致数组元素语法引用超出范围的内存,从而导致段错误。

答案 2 :(得分:1)

什么是i - 它不是零

while(in[i] != '\0'){

答案 3 :(得分:0)

您正在使用

while(in[i] != '\0')

但你还没有初始化&#39;我&#39;

所以初始化&#39;我&#39; 0

答案 4 :(得分:0)

在行'while(in [i]!='\ 0')' - 'i'的值未初始化。请确保在代码中使用它们之前初始化任何变量。这是一个最佳实践我已经看到由于这个错误导致整个功能被破坏。

相关问题