Re: segmentation fault




"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;
}



.



Relevant Pages

  • Re: segmentation fault
    ... Each time I compile ... and execute the following program I get segmentation fault. ... while)!= EOF) ... Posted via a free Usenet account from http://www.teranews.com ...
    (comp.lang.c)
  • Re: segmentation fault
    ... Each time I compile and execute ... int main ... while((c = getchar())!= EOF) ...
    (comp.lang.c)
  • segmentation fault
    ... Each time I compile and execute ... int main ... while((c = getchar())!= EOF) ...
    (comp.lang.c)
  • Accessing hardware using the PCI BUS - problems
    ... and return a pointer to it and use it in C++. ... I use nasmto compile the assembly source ... example creates a segmentation fault. ...
    (comp.lang.asm.x86)
  • Re: HLA 1.81 - HelloWorld example segmentation fault on compile
    ... One thing that I did find, a bit bizarre, is that when I used the ... hlaparse command on my hw.hla file (just for kicks, ... actually did compile the code properly a few times. ... giving me output and returned to the same segmentation fault errors, ...
    (comp.lang.asm.x86)