细分错误:布尔表达式 [英] Segmentation fault: boolean expression

查看:65
本文介绍了细分错误:布尔表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误细分错误。我认为这与isdigit(argv [i])行有关,但不了解原因。

I am getting an error "Segmentation fault". I think it has to do with the isdigit(argv[i]) line but do not uderstand why.

#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

//implement commandline arguments
int main(int argc, string argv[])
{
    //convert string element in array to integer
    int key = atoi(argv[1]);
    //check that user input for key is no more than 2 memory spots, not a negative number and a single input
    if (argc != 2 || key < 0 || isdigit(argv[1]))
    {
        printf("useage: ./caesar key\n");
        return 1;
    }
    else
    {
    string plaintext = get_string("plaintext: ");
    printf("ciphertext: %s \n", plaintext);
    return 0;
    }
}


推荐答案

假设您实际上确实有一个有效的字符串作为参数传递,然后 argv [1] 引用该整个字符串。要检查该字符串的第二个 character 是否为数字,您需要进一步解除引用该字符串,并使用: isdigit(argv [1] [1])。或者,对于该字符串的 first 字符,使用 argc [1] [0] (或 * argv [1 ] )。

Assuming you do actually have a valid string passed as an argument, then argv[1] refers to that entire string. To check if the second character of that string is a digit, you need to further 'dereference' that string, and use: isdigit(argv[1][1]). Or, for the first character of that string, use argc[1][0] (or *argv[1]).

这篇关于细分错误:布尔表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆