How can multiple variables be passed to functions in C?

I am working on an embedded system that has different output options (digital output, serial, analog, etc.). I am trying to find a clean way to pass many variables that will control these functions.

I don’t need to transmit them too often, but I was hoping to get a function that will read the input data (in this case from the TCP network) and then analyze the data (IE, the 3rd byte contains states of 8 digital outputs (according to which the bit in this byte is high or low)) and puts it in a variable where I can then use it elsewhere in the program.

I wanted this function to be separated from the main () function, but for this it would be necessary to pass pointers to about 20 or so variables to which he wrote. I know that I can make variables global, but I'm trying to make it easier to debug by making it obvious when functions are allowed to edit this variable by passing it to the function.

My best idea was a structure and just passed a pointer to it, but was not sure if there was a more efficient way, especially since there is only one function that would have to access all of them at once, while most others only part of the information that will be stored in this group of state variables is required.

Anyway, is there a clean way to send many variables between functions at once that need to be edited?

+5
source share
5 answers

Using a structure with a pointer to it is a really good bet. The code may be a little longer, but it will look beautiful. You can pass a structure by value, but use a link to avoid copying data.

Another alternative is to create a structure consisting of structures. Then you can be more selective in what data you pass to each function, passing all this, just one or two of them, or a separate element of the structure.

typedef struct a {
  struct b {
    int b1;
    int b2;
  } b_s;
  struct c {
    int c1;
    int c2;
  } c_s;
} a_s;
+5
source

This classic use of the “parameter block” is simply a pointer to a well-known structure.

: , ; ( "" ); ( ); , , .

: , , - ; , (, , !); ( , ...)

+2

, - . , . 3- , . , .

0

, , . , , , , , .

0

"" . , . , :

struct func_params {
    int type;
    union {
        struct {
            int    port;
            int    direction;
            long   target_addr;
        } digital_io;
        struct {
            int    channel;
            long   sample_rate;
        } analog_in;
        struct {
            int    channel;
            int    async;
            int    hold_output;
        } analog_out;
        struct {
            int    port;
            int    direction;
            int    encoding;
        } serial_io;
    };
};

my_function (struct func_params* pStruct, size_t length)

, , , ( , ). . (length - sizeof(struct func_params) bytes) . type , , , .

This may be a little more complicated than what you had in mind. In the embedded world, many communication protocols (such as SCSI) exchange messages by sending a header followed by a variable-length data section like this. This gives the functions a clean interface, does a good job of linking related information together, and makes it easy to change functions in the future without changing call parameters.

0
source

All Articles