Re: segmentation fault



sid wrote:

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)

This should use "argc-1". argv[argc] is always NULL. Your system
apparently faults on passing NULL to strcmp (which is allowable).

{
while((c = getchar()) != EOF)
putchar(tolower(c));
}
else
{
while((c = getchar()) != EOF)
putchar(toupper(c));
}
return 0;
}

Most systems will never have "argc == 0" since argc[0] is used to
transmit the name of the running program. Not always so, though.

Tip: Don't use tabs, use spaces, especially in Usenet postings.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net



--
Posted via a free Usenet account from http://www.teranews.com

.



Relevant Pages

  • Re: segmentation fault
    ... Each time I compile and execute ... the following program I get segmentation fault. ... while((c = getchar())!= EOF) ...
    (comp.lang.c)
  • Re: Problem implementing sockets with C++
    ... code presented in the magazine because it won't compile (at least on ... correctionsand do not get a segmentation fault. ... =A0I did some changes mostly to the send and receive routines to fir the ...
    (comp.unix.programmer)
  • Re: Problem implementing sockets with C++
    ... Gdb does not give me this information about the sock ... code presented in the magazine because it won't compile (at least on ... correctionsand do not get a segmentation fault. ...
    (comp.unix.programmer)
  • 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)