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.

Shiv Kumar
  • 9,599
  • 2
  • 36
  • 38
  • `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? – Retired Ninja Aug 22 '17 at 13:24
  • 3
    A simple constructor or in-class initialiser both [work for me](http://coliru.stacked-crooked.com/a/d7e6322a16ffe931). Could this be a problem with your toolchain? Are you passing the correct C++ version flag (at least 11 is required). – Angew is no longer proud of SO Aug 22 '17 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??? – Lundin Aug 22 '17 at 14:30
  • Use std::string instaed of a char array. –  Aug 22 '17 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 – Shiv Kumar Aug 22 '17 at 16:44
  • @manni66, I'm on an embedded system, and don't have that option. – Shiv Kumar Aug 22 '17 at 16:45
  • @RetiredNinja, I see "=5" but I'm guessing it could change. – Shiv Kumar Aug 22 '17 at 16:46
  • @Angew, thnka you for the code sample! That certainly explains how to write the initializer for a char array (something I was stuck at). – Shiv Kumar Aug 22 '17 at 17:13
  • It would seem this question is a duplicate of https://stackoverflow.com/questions/5643923/c-initializing-non-static-member-array or am I missing something here? – Lundin Aug 23 '17 at 06:48

1 Answers1

-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?

IKY.LI
  • 189
  • 4