Re: segmentation fault
- From: "Army1987" <please.ask@xxxxxx>
- Date: Sat, 30 Jun 2007 17:43:10 +0200
"sid" <kingsiddharth@xxxxxxxxx> ha scritto nel messaggio news:1183217437.329750.227550@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi!
I am using gcc (GCC) 4.0.3 (Ubuntu 4.0.3-1ubuntu5) on an Ubuntu 6.06
64bit version on an AMD 64bit machine. Each time I compile and execute
the following program I get segmentation fault. Please tell me whether
the code I have written has some errors or whether there is problem
with my OS or the compiler.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc,char *argv[])
{
int c;
if(argc == 0)
printf("\nNo Arguments\n");
else if(strcmp(argv[argc],"lower") == 0)
argc is the number of arguments plus one.
argv[0] is the program name. argv[1] is the first argument,
and so on up to argv[argc-1] which is the last argument.
argv[argc] is a null pointer, trying to use what it points to (as
you did) causes undefined behaviour (usually a segmentation fault).
eg. If the program is called with
../a.out arg1 arg2 arg3
argc is 4, argv[0] is "./a.out", argv[1] is "arg1", argv[2] is
"arg2", argv[3] is "arg3" and argv[4] is NULL.
{
while((c = getchar()) != EOF)
putchar(tolower(c));
}
else
{
while((c = getchar()) != EOF)
putchar(toupper(c));
}
return 0;
}
.
- References:
- segmentation fault
- From: sid
- segmentation fault
- Prev by Date: Re: Machine epsilon: conclusion
- Next by Date: Re: segmentation fault
- Previous by thread: segmentation fault
- Next by thread: Re: segmentation fault
- Index(es):
Relevant Pages
|
|