I was doing the Vigenere cipher and wanted to translate all the characters into ints 0-25 to be used as the key. I got floating point exception core dumped error. I did an inspection of my code and found the error to be a division by 0. I manage to identify and replicate this error in the code below:

for (int i = 0, j = strlen (argv[1]); i < j; i++)
    if (isupper(argv[1][i]))
        argv[1][i] = argv[1][i] % 'A';
    else if(islower(argv[1][i]))
        argv[1][i] = argv[1][i] % 'a';
        printf("Keyword must only contain letters A-Z or a-z.\n");
        return 1;

I'm not sure why this causes the strlen of argv[1] to become 0 though. Could someone help me to understand? Thanks!

  • Did you try removing the space between the strlen and the (argv[1]) ? You can always try printing whats in argv[1]. Nov 6 '14 at 15:45
  • I removed the space but it is still zero. Printing argv gives me the intended result. e.g. argv[1][1] which is 'A' or 'a' becomes 0. But the strlen is 0. No idea why this is happens.
    – Lucas
    Nov 6 '14 at 15:56
  • strlen returns 0 in case the passed string is an empty string (i.e., "" — a string with 0 characters). And your result is wrong because your ciphering formula is unfortunately incorrect.
    – kzidane
    Nov 6 '14 at 16:06
  • Sorry, I check again and it does return the required values. so typing ./vigenere abch and printing argv[1][1], argv[1][2], and argv[3] gave me 127. Which is correct. This cipher is correct because later on I just have to add it to the users input. But the only error is the strlen of 0. The string passed is not empty since I can get these values.
    – Lucas
    Nov 6 '14 at 16:28
  • To debug, I printed strlen right at the start of the code. it returned 4. But when I printed strlen after running the above program, strlen became 0.
    – Lucas
    Nov 6 '14 at 16:29

causes the strlen of argv[1] to become 0

strlen() reads through a char array and ends when it finds a 0. If you change any of the chars in your key to 0, then strlen() will stop counting when it hits that.

So if your first char is 0, strlen will be 0.

You should create a variable to hold the string length before your loop and then use that in your loop, rather than calculating it each time through the loop.

To answer your question in the comment about altering argv[]:

In general, it's not a good idea to alter argv[] because of how it is stored in memory. The C language does not specify where command line arguments will be stored so you run a risk of overwriting something else that is part of your executable depending on the computer's implementation.

So it's safer to capture the command line arguments and store them in your own variables if you plan on altering them.

  • Yes, I solved this by adding a variable that stores the strlen() first. Thanks for helping me understand this @curiouskiwi! So this means when there is an 'a' or 'A' in the key, it will cause strlen() to be 0. Also, i've read that its not good practice to change argv in this way because of memory sequence issues. But it didn't explain why? Could you help?
    – Lucas
    Nov 7 '14 at 5:06
  • @Lucas, see my edited answer.
    – curiouskiwi
    Nov 7 '14 at 21:10
  • Thanks. I understand it now!
    – Lucas
    Nov 8 '14 at 5:47

You must log in to answer this question.

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