Question:

Very Simple C Program Drives Me Crazy! Can you help please?!?

by  |  earlier

0 LIKES UnLike

Here is my program:

--------------------------------------...

#include<stdio.h>

int main(int argc, char *argv[])

{

char Buffer[10];

if(argc < 2)

{

printf("Put an option\n");

exit(1);

}

strncpy(Buffer,argv[1],9);

if(!strcmp(Buffer,"?random"))

printf("They want help\n");

else if (!strcmp(Buffer,"/A"))

printf("Option\n");

else if (!strcmp(Buffer, "/B"))

printf("BBBBBBBBBBB\n");

else

printf("WRONG\n");

}

--------------------------------------...

lets say the file name elseif.c

After compiling, i can say this program works. However if i change it to:

--------------------------------------...

#include<stdio.h>

int main(int argc, char *argv[])

{

char Buffer[10];

if(argc < 2)

{

printf("Put an option\n");

exit(1);

}

//changed argv[1] to argv[2]

strncpy(Buffer,argv[2],9);

if(!strcmp(Buffer,"?random"))

printf("They want help\n");

else if (!strcmp(Buffer,"/A"))

printf("Option\n");

else if (!strcmp(Buffer, "/B"))

printf("BBBBBBBBBBB\n");

else

printf("WRONG\n");

}

It compiles, but when i execute i get message says

Segmentation fault

Can you please explain to me why this happens?

Im new to C, i try to play with codes to see how it works...

 Tags:

   Report

4 ANSWERS


  1. You have to put an test in before the strncpy() function is called.

    If you aren&#039;t typing in something like this at the command line, then you can&#039;t access the argv[2] parameter.

    myprogram.exe param1 param2

    (where of course, param1 is whatever you want, and param2 is whatever you want)

    switch (argc)

    {

    // This is the case wherein only the program name is set

    // You can reference the argv[0] for a program.

    case 1:

    break;

    // This is the catch-all (so if they entered one or more parameters,

    // then take the last parameter only to use it for the strncpy function.

    default:

    strncpy(Buffer, argv[argc - 1], 9);

    break;

    }


  2. If your argc &lt; 2 that means there are either 1 or zero args, as  Argv 0 is by default the name of the program itself. So your argc (argument count) is always going to be at least 1. If you have one other argument, the count will be 2, but the argv number will 1 (as C counts from zero).

    In your first code, you are reading this argv[1]  to get the buffer input, so the program works.

    In the second code, you are trying to read  argv[2] which does not exist in your code. However, the program will try to find one to read, therefore read into a memory segment that has not been allocated to it. (The buffer gets memory allocated by the ..... char Buffer[10];... statement).


  3. C begins its numbering with 0.  Thus, Argv[0] would be a pointer to the string which is the executable name (elseif in this case).  Argv[1] would be a pointer to the first argument you want to send it. Argv[2] is a pointer to the second argument which gives you a total of three arguments.  You only have two arguments in this.  Thus you are talking about grabbing the command line argument which is out of range.  

  4. If you have one argument, then the variable argc is 2. You run your program like.

    &gt;elseif a

    The argument array has two element argv[0] and argv[1]. No such argv[2]. When you tried to get a value, the system will generate a segment fault.

    Good luck,

    peng

    http://www.eptop.com

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions