Char, array and string

Hi,

Coming from a JS background, I’m pretty new to C++ . Can’t seems to find out what’s wrong with this sketch, compile with no errors, however LED shows blinking red and restarted itself, and when it starts, it went red and repeat itself.

I’ve simplify to code, nevertheless I’m trying to store values (string) in an array and eventually, have a result outputs all data in the array. and since I can only output as string, i need to loop through the array and format them into a string. How can I do that?

  int mockSAA = 233;
  int mockSAB = 301;

  char* message[128];
  String result;
  char resultChar[128];

  void setup() {
      for (int i = 0; i < 8; i++) {
          message[i] = "null";
      }
      sprintf(message[6], "%c", mockSAA);
      sprintf(message[5], "%c", mockSAB);

      char ss[32];
      for (int i = 0; i < 8; i++) {
          sprintf(ss, "%d:%c, ", i, message[i]);
          result.concat(ss);
      }

      result.toCharArray(resultChar, 65); //max length
      Spark.variable("message", resultChar, STRING);
  }

  void loop() {
  }

Cheers,
Edwin

Here you create an areay of 128 pointers to some char* values, but you don't allocate any space for the actual strings. I guess that's not what you want

And here you set the value of eight of these pointer to a memory location in flash (where the compiler placed the string literal "null") and then attempt to write to this read only memory

This will probably be the spot where you get the hard fault (red SOS blink followed by one flash)

If you change your code this way, it should work better

  ...
  char message[8][128];
  String result;
  char resultChar[128];

  void setup() {
      for (int i = 0; i < 8; i++) {
          strcpy(message[i], "null");
      }
  ...