鱼C论坛

 找回密码
 立即注册
查看: 684|回复: 1

求助!想知道为什么这段代码运行的时候总是显示Segmentation fault。

[复制链接]
发表于 2020-7-8 09:25:51 | 显示全部楼层 |阅读模式
5鱼币
这段代码在argv[1]的位置应输入的是数字,但是每次运行的时候,比如./ caesar 2,就会显示Segmentation fault。豪无头绪,请大神帮忙!


  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <ctype.h>

  7. int main(int argc, string argv[])
  8. {
  9.      if (argc == 2 && isdigit(argv[1]))
  10.      {
  11.          int key = atoi(argv[1]);
  12.          string plaintext = get_string("plaintext: ");
  13.          printf("ciphertext: ");
  14.          for (int i = 0, len = strlen(plaintext); i < len; i++)
  15.          {
  16.              char c = plaintext[i];
  17.             
  18.              if (isalpha(c))
  19.              {
  20.                  if (islower(c))
  21.                  {
  22.                      printf("%c", (((c - 97) + key) % 26) + 97);
  23.                      
  24.                  }
  25.                  if (isupper(c))
  26.                  {
  27.                      printf("%c", (((c - 65) + key) % 26) + 65);
  28.                      
  29.                  }
  30.                  else
  31.                  {
  32.                      printf("%c", c);
  33.                      
  34.                  }
  35.              }
  36.          }
  37.          return 0;
  38.      }
  39.      else
  40.      {
  41.          printf("Usage: ./caesar key\n");
  42.      return 1;
  43.      }
  44. }

复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-7-8 09:38:00 | 显示全部楼层
  1. get_string
复制代码

是什么,不记得有这个库函数。

segmentation fault 通常都是修改非法地址导致的。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 16:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表