0

Hello I have the following code:

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); } 

But getting the following error on the runningState->temperatures-> lines:

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

I have also tried variations:

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

And other variations based off what I saw in this answer: C pass a nested structure within a structure to a function?

But without much luck

6
  • 1
    The code you show isn't valid C code. C doesn't allow structure inline initialization like you have. Are you perhaps building C++ code? What is the full file-name of the source file? How do you build it? And about invalid code, that printf call is wrong as well. Commented Oct 28, 2021 at 6:43
  • 1
    On another note, if you want small integers use the type int8_t instead. Or just use plain int. Commented Oct 28, 2021 at 6:45
  • @Someprogrammerdude sorry! Clicked the wrong tag. Have updated appropriately! Commented Oct 28, 2021 at 6:59
  • 1
    Then I recommend you stop using pointers when passing structures, and instead start using references. And you also don't need the struct keyword when declaring variables, as the structure tag-name is a type-name similar to e.g. int. And don't use printf to print but std::cout. Commented Oct 28, 2021 at 7:08
  • 1
    Also since you're programming in C++ you should be careful also with int8_t since it's usually based on char, which means when you print it with std::cout and << it will be printed as a characters not a small integer. Use plain int even for small integers. And do input-validation to make sure that users don't enter invalid or out-of-range values. Commented Oct 28, 2021 at 7:10

1 Answer 1

2

In test, the local argument variable runningState (not to be confused with the global variable of the same name) is a pointer to a structure object, so the arrow operator -> is the correct to use to access its members.

But runningState->temperatures is not a pointer, it's an actual structure object. Therefore you must use the dot . to access its members:

runningState->temperatures.lowTempSetting++; // ^ // Note using dot here 
Sign up to request clarification or add additional context in comments.

2 Comments

Yep, this code was trimmed down and taken from 3 separate files. When I try runningState->temperatures.lowTempSetting++ I get request for member 'temperatures' in 'runningState', which is of pointer type 'runningState_t*' (maybe you meant to use '->' ?).FYI, I have this struct defined in a header that both cpp files include at the top. The struct runningState_t runningState; is at the top of the main.cpp file.
Actually your answer is correct. My VSCode wasn't updating the "problems" it was "detecting", but it compiled just fine when trying it. What tipped me off is that it was having compiler errors on a comment. Uhhg. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.