将嵌套结构传递给函数

Pass nested struct to a function

您好,我有以下代码:

struct temperatures_t {
  char lowTempSetting = 18;
  char highTempSetting = 26;
  char currentTemp = 23;
};

struct runningState_t {
  struct temperatures_t temperatures;
};

struct runningState_t runningState;

void test(runningState_t *runningStateVar) {
  runningStateVar->temperatures->lowTempSetting++;
  runningStateVar->temperatures->currentTemp = 10;
  printf(runningStateVar.temperatures.lowTempSetting);
}

void main() {
  test(&runningState);
}

但在 runningState->temperatures-> 行出现以下错误:

{
"message": "operator -> or ->* applied to \"temperatures_t\" instead of to a pointer type"
}

我也试过变体:

  &(runningState)->temperatures->lowTempSetting++;

以及基于我在此答案中看到的内容的其他变体:C pass a nested structure within a structure to a function?

但运气不佳

test中,局部参数变量runningState(不要与同名的全局变量混淆)是指向结构对象的指针,所以箭头运算符-> 是访问其成员的正确方法。

但是runningState->temperatures不是一个指针,它是一个实际的结构对象。因此,您必须使用点 . 来访问其成员:

runningState->temperatures.lowTempSetting++;
//                        ^
//      Note using dot here