How can multiple variables be passed to a function cleanly in C?
Posted
by aquanar
on Stack Overflow
See other posts from Stack Overflow
or by aquanar
Published on 2010-04-12T21:09:00Z
Indexed on
2010/04/12
21:13 UTC
Read the original article
Hit count: 176
I am working on an embedded system that has different output capabilities (digital out, serial, analog, etc). I am trying to figure out a clean way to pass many of the variables that will control those functions.
I don't need to pass ALL of them too often, but I was hoping to have a function that would read the input data (in this case from a TCP network), and then parse the data (IE, the 3rd byte contains the states of 8 of the digital outputs (according to which bit in that byte is high or low)), and put that into a variable where I can then use elsewhere in the program.
I wanted that function to be separate from the main() function, but to do so would require passing pointers to some 20 or so variables that it would be writing to. I know I could make the variables global, but I am trying to make it easier to debug by making it obvious when a function is allowed to edit that variable, by passing it to the function.
My best idea was a struct, and just pass a pointer to it, but wasn't sure if there was a more efficient way, especially since there is only really 1 function that would need to access all of them at once, while most others only require parts of the information that will be stored in this bunch of state variables.
So anyway, is there a clean way to send many variables between functions at once that need to be edited?
© Stack Overflow or respective owner