this program giving segmentation fault error

December 17, 2010 at 11:48:51
Specs: Linux i686
#include<stdio.h>
#include<grp.h>
#include<sys/types.h>
#include<string.h>
#include<unistd.h>
int main(char *arg,char *argc[])
{
struct group *g;


if(strcmp(arg,"-u")==0)
{
printf("done");
while((g=getgrent())!=NULL)
{
printf("%s\n",g->gr_name);
}

}
return 0;
}


See More: this program giving segmentation fault error

Report •


#1
December 17, 2010 at 12:07:53
I'm not sure what the program is meant to do, but you have the signature of "main()" all wrong. It should be

int main(int argc, char *argv[])

and don't forget that argv[0] is the program name.

The best way to track down segmentation faults is to compile with the -g flag and then run the program under gdb. Set a breakpoint at "main" and single-step through until the program fails.


Report •

#2
December 17, 2010 at 13:08:18
What I notice at a glance:
int main(char *arg,char *argc[])
Should be:
int main(int argc, char *argv[])

I assume you changed it because such a change would make this line compile:
if(strcmp(arg,"-u")==0)
But you need to fix this line, instead of trying to perform string comparisons to some memory address under, let's say, 0x5.

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question