2016-09-22 11 views
-1

-u exを呼び出そうとすると、このエラーが発生します。 "./Randoms -u 100"このコードで何が問題になりますか?セグメンテーションフォールト:11

他のすべてのケースは正常に動作します。なぜそれが起こっているのですか?どうすれば修正できますか?

私は下限-lと上限 -u間のランダムな数字を印刷しようとしています。すべてがコマンドライン引数として送信されます。

#include <stdio.h> 
    #include <stdlib.h> 
    #include <unistd.h> 
    #include <ctype.h> 
    #include <string.h> 
    #include <time.h> 

    static char *prgn; 

    int main(int argc, char *argv[]) 
    { 
     srand(time(NULL)); 
     int randomnumber; 
     int nums = 10; 
     int l = 1; 
     int u = 100; 
     int c = 0; 
     int error = 0; 
     char *options = ":n:l:u"; 
     prgn = (char *)malloc(sizeof(char)*(strlen(argv[0])+1)); 
     strcpy(prgn,argv[0]); 
     while ((c = getopt(argc,argv,options)) != -1) { 
      if (c == '?') { 
       c = optopt; 
       printf("illegal option %c\n",c); 
      } else if (c == ':') { 
       c = optopt; 
       printf("missing value for option %c\n",c); 
      } 
      else { 
     switch (c) { 
      case 'n': 
       nums = atoi(optarg); 
       break; 
      case 'u': 
       u = atoi(optarg); 
      break; 
      case 'l': 
       l = atoi(optarg); 
      break; 
     } 
     } 
    } 
     while (nums !=0){ 
        randomnumber = l + rand() % (u - l); 
        printf("%d\n", randomnumber); 
        nums--; 
     } 
c = 0; 
return 0; 

} 
+0

によって

char *options = ":n:l:u"; 

を置き換えますか?それはどこでクラッシュしたのですか?オプションに 'u:'は必要ないのですか? – John3136

+0

これで4時間以上を費やして大胆な結腸を逃した。 kms。ありがとうございました! –

+0

申し訳ありませんが、この問題は既に解決されています。 – clockley1

答えて

1

はあなたがそれをデバッグするとき、あなたは何を見つけた

char *options = "n:l:u:"; 
関連する問題