We check the string validity when we use the get_string() function. Example: if (p != NULL) Are we also supposed to check string validity when the string is entered as a command line argument?
1 Answer
If you're using command-line arguments, always test argc
before you use argv
, as otherwise you might access an array element that's not there.
Obviously, you cannot repeat input of a command-line argument using a do-while loop.
If you are on vigenere, you must reject password strings not entirely alphabetic, as that's part of the specification.
In most cases, the specification should tell you how to handle command line arguments. Otherwise, try to make it so your programme will behave in some reasonable way (a strange file name might not be a problem if the failing of fopen is handled appropriately, but it might be a problem if you give access to files the user should not be able to access)
-
Yes I have always tested argc before argue. I have already completed vigenere. And yes I checked if strings are not entirely alphabetic. I am on crack right now. I was asking just for curiosity whether is it possible for a user to enter an invalid string (which produces NULL) straight from the command line. I mean if we are checking a user inputted string for validity, we should also check the strings inputted by the user in the command line. I am not asking this question in regard to the cs50 problem sets, just wanted to know the general practice. Feb 24, 2017 at 13:37
-
It should not be possible to create a NULL entry in argv, but of course you could pass an empty string, which has
'\0'
as its first character.– BlauelfFeb 24, 2017 at 14:08 -
How is a NULL entry created in a string anyway? In the lecture it was told that if too large a string is entered it can result in a NULL entry. Can't the same thing take place for an argv entry too? Feb 24, 2017 at 14:24