Been looking for answers to this, but having a lot of trouble. I am receiving a segfault that seems to be coming from the isdigit line, though I'm not sure what is happening to argv[1] to cause the segfault. I am trying to create a function that goes through each character in argv[1] and returns a bool that says whether they are only digits or not.

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

bool only_digits(string argv[1]);

int a;
int x;

int main(int argc, string argv[])
    //verify command line argument
    x = strlen(argv[1]);
    if (only_digits(&argv[1]) == true || argc != 2)
        printf ("Usage: caesar key\n");
        return 1;

    //convert argv to int
    a = atoi (argv[1]);

    //get message
    if (only_digits(&argv[1]) == false && argc == 2)
        string plaintext = get_string("Text: ");
        printf("%s\n", plaintext);

    //rotate message by argv


bool only_digits(string argv[1])
    int b;
    for (int i = 0; i < x; i++)
        b = isdigit(argv[1][i]);
    return b;

The rest of the code for the problem is not done, but I am getting stuck on verifying the the command line argument is only digits.

1 Answer 1


The segfault is coming because there does not exist argv[1] at this line b = isdigit(argv[1][i]);

Wait what????

The function definition declares a string array with 1 element. The index of that element is 0.

One option is to change the name of the argument in only_digits function declaration; it needn't be (shouldn't be?) an array. (And of course change it wherever used).

This will get rid of the segfault. But it will uncover other problems.

  • Review man isdigit return value; it is not a boolean.
  • The only_digits function will only report on the last character in the string to be tested, because b gets a value assigned for each i in the loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .