Is there a way to use a typedef struct variable in a function…? [closed]

As the code in the question is currently written, no, function cannot access the variable boo.

You would either need to pass a pointer to boo as a parameter to function:

int main( void )
{
  ...
  function( string, &boo );
  ...
}

void function( char *str, a *b )
{
  ...
}

or you would need to declare boo at file scope (outside the body of either main or function):

a boo;

int main( void )
{
  ...
  function( string );
  ...
}

void function( char *str )
{
  // do something with str and boo
}

or, have a global pointer that you set to point to boo:

a *ptr;

int main( void )
{
  ...
  ptr = &boo;
  ...
  function( string );
  ...
}

void function( char *str )
{
  // do something with str and *ptr
}

Otherwise, boo is not visible to function.

EDIT

As user3386109 points out, the typedef isn’t relevant here – the answer is the same regardless of how boo is declared, or whether its declared using a typedef name or not.

Leave a Comment