0

I’m having problems understanding how to get elements from multidimensional array in C++. The code runs on my particle photon, and it doesn’t seem like I have any compiling errors, but when I check my printed value, I’m expecting to get the return value of 1000. Instead, I get what seems to be a consistent random value of -1069809521. I know that the problem area is TotalTime += (*FaceEmoteTimeArray[FaceNumber][i]); but am unsure why. The full code:

const int FaceEmoteTime0[31] = {143, 143, 143, 142, 143, 143, 143};
const int FaceSize0 = sizeof(FaceEmoteTime0) / sizeof(FaceEmoteTime0[0]);

const int* FaceEmoteTimeArray [][FaceSize0] = {FaceEmoteTime0};
const int FaceSizeArray[] = {FaceSize0};

int TotalTime;
int Plays;
int FaceNumber = 0;

void setup() {
  Serial.begin(115200);
}

void loop() {
  if (Plays == 0) {
    for (int i = 0; i < (FaceSizeArray[FaceNumber]); i++) {
      TotalTime += (*FaceEmoteTimeArray[FaceNumber][i]);
    }
    Serial.println(TotalTime);
    Plays++;
  }
}
hahman
  • 21
  • 1
  • 1
  • 2
  • Can you use a debugger on the particle-photon? – RoQuOTriX Apr 29 '20 at 11:51
  • What do you expect `const int* FaceEmoteTimeArray [][FaceSize0] = {FaceEmoteTime0};` does? Why a 2D array of pointers with only one row? – David C. Rankin Apr 29 '20 at 11:56
  • @RoQuOTriX, you can only use the Verify button. – hahman Apr 29 '20 at 11:57
  • @David C. Rankin I'm simplifying my question, in reality I have 25 arrays I'd like to add to FaceEmoteTimeArray – hahman Apr 29 '20 at 11:59
  • OK, what is happening is `FaceEmoteTime0[31]` has 31 elements, the first 7 initialized `{143 ... 143}` the rest are zero. `FaceSize0` is `31`. `FaceEmoteTimeArray` is a 1 x 31 2D array of *pointers* with the first initialized to the first element of `FaceEmoteTime0` the rest are `nullptr`. `FaceSizeArray[]` is a one-element array with its only element set to `31`. `for (int i = 0; i < (FaceSizeArray[FaceNumber]); i++)` invokes *Undefined Behavior* on the first iteration. Likewise with `*FaceEmoteTimeArray[FaceNumber][i]` which is 30 rows past the only row it has. (bad things will happen fast) – David C. Rankin Apr 29 '20 at 12:05
  • doing a bit more digging, when I print FaceEmoteTimeArray[FaceNumber][0] I get the correct 143. However, FaceEmoteTimeArray[FaceNumber][1] give me the value 537001984. This number also appears for [FaceNumber][2], [FaceNumber][3], [FaceNumber][4], [FaceNumber][5], [FaceNumber][6]. – hahman Apr 29 '20 at 12:06
  • `FaceSizeArray[FaceNumber])` is `30` elements past the end of the single-element array -- also *Undefined Behavior* -- that was one I missed in my comment, but didn't have room to add. You need to ensure you are declaring the arrays as they are needed. Currently it is clear that there is a problem there.... Start by outputting `FaceSize0`, then know that on access an array is converted to a pointer to its first element. Thus `{FaceEmoteTime0}` initializes the first element to a single pointer and `FaceSizeArray[] = {FaceSize0};` is a one element array initialized to `31`. – David C. Rankin Apr 29 '20 at 12:08
  • A good read of [Array declaration](https://en.cppreference.com/w/cpp/language/array) should clear things up. – David C. Rankin Apr 29 '20 at 12:14
  • @DavidC.Rankin I've gone through the page but I'm still as confused as ever, all I'm trying to do is make 'TotalTime += (*FaceEmoteTimeArray[FaceNumber][i]);' act the same as if the code was 'TotalTime += (FaceEmoteTime0[i]);' – hahman Apr 29 '20 at 13:45
  • Start with what do you want `FaceEmoteTimeArray` to store? (`int` or pointer-to `int`)? Then declare `type FaceEmoteTimeArray[num_of_rows][FaceSize0];` so you have the number of rows in that array you need. If you don't need more than 1 row, then `type FaceEmoteTimeArray[FaceSize0];` Do the same for `const int FaceSizeArray[number_of_elements] = {FaceSize0};` so the array has the number of elements you need. Then initialize all arrays. You initialize fewer than the number of elements in each array, and allowing default initialization of the rest. Fill with needed values - then loop/sum. – David C. Rankin Apr 29 '20 at 13:58
  • Take your code apart. Start with just the `FaceEmoteTime0[31]` array. Get to where you can initialize it and print out each of the values. Then add the next array, validating each as you add them to your code -- less confusing that way. Break the problem down into smaller and smaller pieces until you can get each piece to work -- then start putting them together. – David C. Rankin Apr 29 '20 at 14:06

0 Answers0