
23-10-2005, 21:01
|
 |
 |
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
|
|
|
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.)
- 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)
- 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.
- 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
|