ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
printf("Options are:\n");
printf("1:\t Add syncookies support (Assuming it's compiled)\n");
printf("2:\t Close certain ports (-h ports for list)\n");
printf("3:\t ENd.\n");
return 0;
}
I'm sure it would be done easily with anothere language but how will that improve my C skills?
Program isn't complete, but I would like to get argv working before I tinker with the rest.
char **argv is an array of strings (which are arays of chars).
when ur doing strcmp(*argv,...), what part of the arguments passed at cmd-line are u trying to compare?
for instance:
./pgm_name one two three four
would have
argc = 5
argv[0] = pgm_name
argv[1] = one
argv[2] = two
argv[3] = three
argv[4] = four
so if you want to compare the third argument you pass with "-b", you would have to do a
Lol, after endless amount of tinkering, the segfault has been fixed.
gdb was pointing it's error to strcmp. But I thought, why would strcmp be called if I put no arguments in there? Wouldn't it just run help() and exit? The return 0 would send it back to main() then it would die at strcmp for some reason, so I removed the return 0; in help() and added it after the help() called in main().
Originally posted by vexer doing ./file without options segfaults but added with the -b option, it works no segfault/error
argv[1] points to the first argument, so if you run it without an argument, argv[1] points nowhere. Hence segfault. You should check if there actually is an argument before trying to read it, eg:
Code:
if (argc > 1) {
if(strcmp(argv[1], "-b") == 0) {
printf("You a stinker\n\n\n");
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.