这样也不行。我要传递不单单holding_registers 变量。要所有main函数读出来的数据
时间: 2024-02-11 18:07:53 浏览: 11
好的,我明白了你的要求,你想要传递所有main函数读出来的数据给其他函数使用。你可以将数据存储在一个结构体中,并将结构体作为参数传递给其他函数。这样就可以方便地将所有数据传递给其他函数使用。以下是一个示例代码:
```c
#include <stdio.h>
typedef struct {
int holding_registers;
float temperature;
// 添加其他需要传递的变量
} Data;
void functionA(Data data) {
printf("holding_registers: %d\n", data.holding_registers);
printf("temperature: %f\n", data.temperature);
// 使用其他需要传递的变量
}
void functionB(Data data) {
printf("holding_registers: %d\n", data.holding_registers);
printf("temperature: %f\n", data.temperature);
// 使用其他需要传递的变量
}
int main() {
// 读取数据
Data data;
data.holding_registers = 10;
data.temperature = 25.5;
// 读取其他需要传递的变量
// 传递数据给函数A和函数B
functionA(data);
functionB(data);
return 0;
}
```
在上面的代码中,我们定义了一个包含所有需要传递的变量的结构体Data,然后在main函数中读取数据并传递给函数A和函数B。在函数A和函数B中,我们可以轻松地访问所有需要传递的变量。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)