0

I've seen C++ Zero-out a struct array? and other SO posts. Unfortunately I haven't found anything that can initialize a char array member of a struct.

I have this struct

struct SettingsData {
  ModeType Mode = ModeType::Off;
  char Zone[26];
  double Temperature = 0.0;
  OnOffState Heater = OnOffState::Off;
  OnOffState Airconditioner = OnOffState::Off;
  OnOffState Fan = OnOffState::Off;
  bool ApplianceOn = false;
};

I'm looking for C++ way of doing this. I realize I should use a member initializer lists but I can't figure out how to initialize the char array. I get

array used as initializer

error.

I've even tried

  char Zone[26] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
               '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
               '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};

No compile error, but I'm still seeing unwanted "data" in the Zone member.

9
  • char Zone[26] = {0}; works for me, so does char Zone[26] = {}; but I feel like the 0 is more descriptive. Can you elaborate what unwanted data you're seeing right after you instantiate the struct? Commented Aug 22, 2017 at 13:24
  • 3
    A simple constructor or in-class initialiser both work for me. Could this be a problem with your toolchain? Are you passing the correct C++ version flag (at least 11 is required). Commented Aug 22, 2017 at 13:26
  • Is this C or C++? In C++ you would have to initialize the member through the constructor. char Zone[26] = ... doesn't work for C struct members and it doesn't work for C++ struct members either. So... what are you actually trying to do? Is the code in the question the real code? And what does this have to do with embedded systems??? Commented Aug 22, 2017 at 14:30
  • Use std::string instaed of a char array. Commented Aug 22, 2017 at 16:43
  • @Lundin, this is C++11. I don't know the C++ initializer syntax for char[]. As I mentioned in my post, I get an error - array used as initializer Commented Aug 22, 2017 at 16:44

1 Answer 1

-1

Some embedded toolchains require you to call a built-in function to initialise data as part of the startup function, which will then copy the initialisation data from ROM to the working memory space in RAM.

For example, with Renesas RX platform, _INITSCT() function needs to be called. According to compiler documentation:

The initialization routine for RAM area sections (_INITSCT) is called. Uninitialized data sections are initialized to zero. For initialized data sections, the initial values of the ROM area are copied to the RAM area. _INITSCT is provided as a standard library.

Could this apply to your system as well?

Sign up to request clarification or add additional context in comments.

1 Comment

This has absolutely nothing to do with the compiler error which the question is about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.