View Single Post
  #9   Spotlight this post!  
Unread 23-10-2005, 21:01
Mike's Avatar
Mike Mike is offline
has common ground with Matt Krass
AKA: Mike Sorrenti
FRC #0237 (Sie-H2O-Bots (See-Hoe-Bots) [T.R.I.B.E.])
Team Role: Programmer
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Watertown, CT
Posts: 1,003
Mike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond repute
Re: Typedef Struct as function input

Quote:
Originally Posted by Ryan M.
Heck, I'm surprised it compiles that far.

Code:
void Func(struct typedef_example *variable);

void Func(struct typedef_example *variable)
{
...
}

typedef struct name_here
{
  int x;
} typedef_example;
typedef_example object_example;

object_example.x = 2;

Func(object_example);
The three things in red illustrate the error. (I think.)
  1. Typedef is used to give a current type a new name. Using typedef here... well, I don't know what it does. (typedef, first Google result)
  2. The declaration of object_example is made using typedef_example, which is actually an instance of the struct. The blue shows you where you instanciated it. (You can do the same thing with C++ classes, which is why you have to have that ending semicolon.) To actually do this, you should give the struct a name (where the yellow is), then use that.
  3. And the one which is definetely causing the error... you forgot the ampersand. (Gets the address of the variable, which is what a pointer holds, vs. the way you have it now which is the actual struct.)
That said, it is possible that C supports this odd annonymous struct typedefed thing, but the way I'd do it would be:

Code:
void Func(struct typedef_example *variable);

struct typedef_example
{
  int x;
};

void Func(struct typedef_example *variable)
{
...
}

struct typedef_example object_example;
object_example.x = 2;
Func(&object_example);
Thanks a ton, it works now =D
__________________
http://www.mikesorrenti.com/