segmentation fault

S

sid

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

Army1987

sid said:
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.
 
H

Harald van =?UTF-8?B?RMSzaw==?=

sid said:
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;
}
 
M

Malcolm McLean

sid said:
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)
{
while((c = getchar()) != EOF)
putchar(tolower(c));
}
else
{
while((c = getchar()) != EOF)
putchar(toupper(c));
}
return 0;
}
The bug is almost never with the compiler, particuarly when the language is
a stable one like C.
In this case you've forgotten that C counts from 0 to N-1 in array indices.
 
C

CBFalconer

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top