Log in

View Full Version : Pointers to structs?


Jeff McCune
16-02-2004, 18:28
I'm trying to abstract relays into a structure with a forward and reverse element. The idea is to have a single set_relay() funtion that takes a pointer to this data structure as it's input.

I can't figure out how to get mcc to accept my syntax. I've been referencing a lot of documents, so I'm pretty sure my syntax is correct, but I haven't verified it with gcc or what not. I'm going to go do that now and then post some of my example code. Was just wondering if there's problems to start with though.

Joe Ross
16-02-2004, 18:39
We had this discussion (well, not really a discussion, but a comment) in another thread. http://www.chiefdelphi.com/forums/showpost.php?p=217459&postcount=19

depending on how you are doing it, you may need to make your own macros.

Jeff McCune
17-02-2004, 13:17
After some testing in gcc, I figured it out. The more descriptive errors helped. The page I was reading was incorrect about the ordering of the type name versus the variable name. I'm actually passing a bitmask value that I #defined as a bitmask, but I just did it this way so it's easier for people to understand the pointer stuff that's going on. It's so nice having full ansi C.


struct relay_type { int fwd; int rev};
struct relay_type ball_arm_lower;

void set_relay (struct relay_type *relay, int fwd, int rev)
{
relay->fwd = fwd;
relay->rev = rev;
}

main()
{
update_inputs ();
set_relay (&ball_arm_lower, 1, 0);
update_outputs ();
}