I want to assing argv[1] (or argv[2]) to char *file:
It's segmentation fault (of course). But I can't think of how to do this (I'm tired or something :P)Code:file = &(*argv[1]);
I want to assing argv[1] (or argv[2]) to char *file:
It's segmentation fault (of course). But I can't think of how to do this (I'm tired or something :P)Code:file = &(*argv[1]);
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
Make sure you argv[1] exists before you access it.Code:char *szFile = argv[1];
That's a segmentation fault too (I'm sure argv[1] exists!)
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
Ideswa's code is actually equivalent to that, though somewhat more convoluted. If it's segfaulting anyway, argv[1] is probably NULL (argv[argc] is always NULL) or beyond the end of the array. Check to make sure it's a valid array index before you use it.
Note that argv[0] (the program name) is guaranteed to exist, so you don't have to check for its existance. I think it will be "" if nothing else, so you're okay printing it. [Someone correct me if I'm wrong.]Code:if(argc >= 2) { file = argv[1]; } else print_usage();
[edit] argc has to be at least 2 for argv[1] to be a parameter. [/edit]
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Ok, please explain why this is segfaulting (to be sure, I changed everything to argv[0]):
Code:char *file; int n; int lines = 0; int curLine = 0; if(argc == 2) { file = argv[0]; n = 5; } else if(argc == 3) { n = atoi(argv[1]); file = argv[0]; } else { perror("Argument error!"); }
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
Write out an entire program. What you posted shouldn't segfault.
BTW, I wouldn't suggest opening argv[0], though. At the very least, be prepared for it to fail.
Because of first using malloc(), I still used free(file) at the end, whichwas causing the segfault! Thanks for the help
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
Something like this would work, because free(NULL), by definition, does nothing. Then you wouldn't have to check argc twice.
Just a thought. Perhaps you're already doing that. Or perhaps you could exit the program when argc is invalid, skipping the free altogether. Or you could just ignore my ramblings and leave it how it is . . .Code:char *file = NULL; if(argc >= 2) { file = malloc(strlen(argv[1]) + 1); strcpy(file, argv[1]); } free(file);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.