Segmentation Fault (core dumped)

Code snippet written in C:
int main(int argc, string argv[])
    char final_cipher_txt[MAXSTRING];
    char temp, temp1;
    int num, i, j, n, x;
    int key = atoi(argv[1]);
    if (argc > 2 || argc < 2)
        return 1;

Program needs to except one and only one command line arg and exit if more than one arg is passed at the prompt (which it does) and exit if no arg is passed. Now, when I enter more than one arg the program exits as it should. When I add the code in the if statement "|| argc < 2" I get a seg fault when I dont enter an arg @ the prompt. I'm lost. Ive tried just about everything I could think of. Any hints on how to make this work in the way that I described?
Why are you accessing argv[1] before you know if it exists?
A good question. However when I ener an arg at the prompt the program works perfectly. I enter, ./caesar 13 (any int actually) and bam, works great. Name of the c file is caesar btw. The program was designed to accept one (int)arg, then use the int as the key for the caesar cipher.
I will admit there are gaps in my knowledge with respect to passing arguments at the prompt. Thats why Im here. Educate me.
Last edited on
So, when you enter an argument, argv[1] is there so it doesn't crash.

Again, why are you accessing argv[1] before you know if it exists? Move line 7 past line 9.
Nice Cire. Thanks, works perfect. *bows*

Topic archived. No new replies allowed.