0

I am returning a segmentation fault with this code. The argc portion of the code is function correctly if i enter too many arguments returning "1". If the correct number of arguments are entered I get a segmentation fault. I am pretty new to this and could use some help figuring out why.

Here is output from testing when i added a print f to the beginning.

./caesar your key is (null) segmentation fault

./caesar 2 your key is 2 segmentation fault

./caesar 2 3 your key is 2 segmentation fault

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

int main (int argc, string argv[])
{
    int counter = 0;

    printf("your key is: %s \n", argv[1]);


    for (int i =0, n=strlen(argv[1]); i<n; i++)//tries to read argv[1] to determine if all of the elements are digits
    {
        if (argv[1][i]<=9)
        {
            counter ++;//adds to counter when decimal digits are found
        }
        else
        {
            counter--;//subtracts from counter if non decimals are found
        }
    }
    if (argc==2 && counter == strlen(argv[1]))//if arguments are entered properly prompt user for message to encode.
    {
       string k = get_string("Plaintext: \n");
    }
    else
    {
        printf("1 \n");
    }
}

I know argv is taking in the second argument stored in argv[1]. I am just not sure how to correct this outcome. Any help would be greatly appreciated.

Thanks

0

The seg fault happens when the program is run without any parameters. It is looking for a value in argv[1]. If there are no parameters given, then there argv[1] is null. Then, when it calls strlen(argv[1]), it throws a seg fault. When a function like strlen (and a lot of related functions) is called with null input, it seg faults.

When I run the code with a key, it executes to the end without any seg faults. Perhaps you haven't recompiled your code since you updated it?

There are a number of other issues in the code, but this question related only to the seg faults. New problems deserve a new question.

Happy coding.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • Cliff, Thank you for your response. I have saved and recompiled and i am still getting segmentation faults. Your answer does bring up some other questions but I think the first is the most important. I am an extreme rookie when it comes to coding so I apologize if this is elementary. Is this a problem of logic. IE is the problem that I am asking for something in argv[1] before i know anuything is there? Have I failed to initialize argv[] correctly. I feel like I have essentially copied this line of code straight from the lecture but if I am missing something can you tell me what it is? – Rob Watt Apr 10 at 14:26
  • No apologies necessary. Everyone starts learning at some point. What may seem very elementary to me now can easily befuddle new programmers! It happened to me too. You're on the right track, although you were kind of dancing around it. YES, you need to verify that there is something in argv[1] ( or argv[x]) before you attempt to use it. If not, you get a seg fault. The way to do that is to check the value of argc. It tells you how many elements there are in the argv array. Remember too that argv is an array that starts at 0, so if argc = 3, there's argv[0],to argv[2] only. No argv[3]! – Cliff B Apr 10 at 19:02
  • awesome! Thank you so much for the help. I just updated my code as such and the segmentation fault is gone. Now on to making the rest of the program work (perhaps easier said than done). I really appreciate you taking the time to help me out. – Rob Watt Apr 10 at 19:42

You must log in to answer this question.

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