Originally posted by MethodMan
I am having some problems with passing structs to functions.
Maybe I am making a common error, which I dont realize.
Thanks.
Chew on this for a while:
Code:
struct mystruct
{
char name[BUFSIZ];
};
void fun1( struct mystruct m )
{
printf("m.name = %s\n", m.name );
}
void fun2( struct mystruct m )
{
printf("m.name = %s\n", m.name );
printf("calling fun1( m )\n");
fun1( m );
printf("fun2() returns\n");
}
void fun3( struct mystruct *p )
{
if( p )
{
memset( p->name, '\0', BUFSIZ );
strcpy( p->name, "Todd" );
printf("p->name == %s\n", p->name );
}
else
printf("You screwed up.\n");
}
}
int main( void )
{
struct mystruct m;
strcpy( m.name, "BillyBob" );
fun2( m );
printf("\nCalling fun3( )\n");
fun3( &m );
fun2( m );
return 0;
}
The first two functions just take by value a structure. The third function takes a pointer to a structure, and modifies its contents, so that, when the other two functions are called again, you see the change.
Quzah.