View Single Post
  #9   Spotlight this post!  
Unread 23-10-2005, 19:34
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: Typedef Struct as function input

Quote:
Originally Posted by Mike
OK, so I've solved some problem and caused some more.

Right now my code is...
I'm getting Error [1146] type mismatch in argument 1 when I try to compile. Any ideas?

EDIT: The error is on Func(object_example);
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);
__________________