Re: segmentation fault



sid wrote:
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.

You're passing argv[argc] to strcmp, but argv[argc] is always NULL. Did you
perhaps mean argv[1]? (If you did, you will also need to change your
argc == 0 check, in case argc is exactly 1.)

#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)
{
while((c = getchar()) != EOF)
putchar(tolower(c));
}
else
{
while((c = getchar()) != EOF)
putchar(toupper(c));
}
return 0;
}
.



Relevant Pages

  • SUMMARY Additional: Open Ldap 2.1.22 compile woes on 5.1a
    ... is using the value getaddrinfo varible from the header file to an int. ... cat> conftest.$ac_ext <<EOF ... >> Has anyone come accross the following problem when trying to compile ... >> For more info on why you should not send html in email go to ...
    (Tru64-UNIX-Managers)
  • Re: Discarding unread data after scanf()
    ... You need to test for EOF. ... stream, so there's no risk of discarding any more than needed. ... getchar() returns EOF on failure or end of file. ... int skip2nl ...
    (comp.lang.c)
  • Re: Whats the deal with the "toupper" family?
    ... plus EOF which is guaranteed not to be in that range. ... Not according to C89. ... According to C89, getchar() is equivilent ... returning "an unsigned char converted to an int". ...
    (comp.lang.c)
  • Re: C newbie problem
    ... while(c = getchar()!= EOF){ ... Your avoidance of typing has led you to write a program without defined behavior under either the old or new standard. ...
    (comp.lang.c)
  • Re: Why getchar() doesnt quit if EOF isnt the first char
    ... Why getchar() doesn't quit if EOF isn't the first char] ... int main ... It's a value returned by getcharto indicate an end-of-file condition; that value is distinct from any character value. ...
    (comp.lang.c)