Click to See Complete Forum and Search --> : In function `main': parse error before `char'


mastersibn
12-14-2000, 11:02 PM
Right. The snippet is actually quite small and sometimes I have related problems that aren't this one. What I need to know is why sometimes I can't set variables inside a function. Function in question:


int main( void )
{
/* Get the home dir. */
struct passwd *pw_entry;
pw_entry = getpwuid( geteuid( ) );

char *BaseDir = EvalBaseDir( pw_entry->pw_dir );
return( 0 );
}

char *EvalBaseDir( char *PwDir )
{
char *BaseDir = strcat( PwDir, "/.gtkak" );
return( BaseDir );
}


Now what makes this annoying is that if I remove the char BaseDir..., and replace it with printf( "%s \n", EvalBaseDir( pw_entry->pw_dir ); then everything works flawlessly.

Gcc is complaining that I'm trying to create this string, but I can't find any other viable ways to do it. Why can't I creat char *BaseDir?

Like I said, sometimes I have problems assigning variables (global vars, for instance, -never- work for me). I'd really like to know why this always fails.

I'm only using the -Wall switch to compile...

thx

------------------
grab my gnupg key (http://jove.prohosting.com/~msibn/sibn-p.asc) if you feel so inclined.


cAPS lOCK? wHAT cAPS lOCK?
I cna ytpe 300 wrods pre mniuet!!!
an operating system has not just advantages...

Stuka
12-15-2000, 12:31 AM
IIRC, in C you must declare all variables before any code, so your initialization of pw_entry on the previous line is killing your variable declaration. This problem doesn't exist in C++, so if you compile that bit w/g++, it should work.

mastersibn
12-15-2000, 02:35 AM
Originally posted by Stuka:
IIRC, in C you must declare all variables before any code, so your initialization of pw_entry on the previous line is killing your variable declaration. This problem doesn't exist in C++, so if you compile that bit w/g++, it should work.

Thanks. That's kind of a stupid limitation to have, IMO. I tried it with g++, and I get errors like
initialization to char * from char lacks a cast
unused variable char * BaseDir

In any case, I'm trying to do this without the ++, so I'm going to have to try to find a workaround somehow or other. Not sure how to do it without the risk of a buffer overflow, but I think I can learn how to use malloc() sonner than later...

thanks. I always wondered why I got errors like that sometimes....

------------------
grab my gnupg key (http://jove.prohosting.com/~msibn/sibn-p.asc) if you feel so inclined.


cAPS lOCK? wHAT cAPS lOCK?
I cna ytpe 300 wrods pre mniuet!!!
an operating system has not just advantages...

miller
12-15-2000, 11:23 AM
Yeah, Stuka had it right. Either use g++, or (my recomendation) declare vars before code. Like this:


int main( void ){
/* declare vars here */
struct passwd *pw_entry;
char *BaseDir;

/* start code here */
pw_entry = getpwuid( geteuid( ) );
BaseDir = EvalBaseDir( pw_entry->pw_dir );
return( 0 );
}

char *EvalBaseDir( char *PwDir ){
char *BaseDir = strcat(PwDir, "/.gtkak" );
return( BaseDir );
}