1
void setup()  {
 Serial.begin(9600);
}

void loop()
{
 time_Text(2);
}

void time_Text(int PastTo_val) {
  switch (PastTo_val) {
   case 1:
   {
    int tPTT_y[] = {11, 11, 11, 11};
    int tPTT_x[] = {6, 7, 8, 9};
  }
  break;
case 2:
  {
    int tPTT_y[] = {11, 11};
    int tPTT_x[] = {10, 11};
  }
  break;
 }

 int n_tPTT = sizeof(tPTT_x) / sizeof(tPTT_x[0]);

 Serial.print(tPPT_y);
 Serial.print(" : ");
 Serial.print(tPPT_x);
 Serial.print(" : ");
 Serial.println(n_tPPT); 

}

I have a case that I need to assign different kind of array value (with different size per case), and then find the size of the array, and print it. The problem is that the tPTT_y and tPTT_x is a local variable which cannot be accessed outside of the case function. I have tried to declare it before the switch function but I cannot give a size number because the size is different per case, how could I solve this?

2 Answers 2

1

Create two global arrays that contain the maximum amount of number you ever need, e.g.

int tPTT_x[4];
int tPTT_y[4];

Than create one global int that contains how much x and y are filled, e.g.

int filled;

Than you can change your cases to

void time_Text(int PastTo_val) {
  switch (PastTo_val) {
   case 1:
   {
    tPTT_y[0] = 11;
    tPTT_y[1] = 11;
    tPTT_y[2] = 11;
    tPTT_y[3] = 11;
    tPTT_x[0] = 6;
    tPTT_x[1] = 7;
    tPTT_x[2] = 8;
    tPTT_x[3] = 9;
    filled = 4;
  }
  break;
case 2:
  {
    tPTT_y[0] = 11;
    tPTT_y[1] = 11;
    tPTT_x[0] = 10;
    tPTT_x[1] = 11;
    filled = 2;
  }
  break;
 }

Than you can use the 'filled' variable to see how many items in the array are 'valid' (0 <= item < filled).

1
  • @MaximilianGerhardt Thanks for the remark (don't have a compiler at hand). I fixed it in my answer. Commented Mar 26, 2018 at 8:43
1

Just create a buffer of a maximum value and copy in the value you want while remembering the length of the array.

#define MAX_ARR_LENGTH (4)

void time_Text(int PastTo_val) {
    int tPTT_x[MAX_ARR_LENGTH];
    int tPTT_y[MAX_ARR_LENGTH];

    const int tptt_y_case_1[] = {11, 11, 11, 11};
    const int tptt_x_case_1[] = {6, 7, 8, 9};
    const int tptt_y_case_2[] = {11, 11};
    const int tptt_x_case_2[] = {10, 11};

    int filled = 0;

    switch (PastTo_val) {
    case 1:
    {
        memcpy(tPTT_x, tptt_x_case_1, sizeof(tptt_x_case_1));
        memcpy(tPTT_y, tptt_y_case_1, sizeof(tptt_y_case_1));
        filled = 4;
    }
    break;
    case 2:
    {
        memcpy(tPTT_x, tptt_x_case_2, sizeof(tptt_x_case_2));
        memcpy(tPTT_y, tptt_y_case_2, sizeof(tptt_y_case_2));
        filled = 2;
    }
    break;
    }

    Serial.println("Printing array values");
    for(int i=0; i < filled; i++) {
        Serial.println("tPPT_x["+ String(i) +"] = " + String(tPTT_x[i]));
        Serial.println("tPPT_y["+ String(i) +"] = " + String(tPTT_y[i]));
    }
}

Depending on whether you need to modify the memory you can also just point to constant memory and remember the length.

void time_Text(int PastTo_val) {
    const int* tPTT_x;  //Pointers to the values
    const int* tPTT_y;

    //You make also add "static" if you want to reference this
    //data beyond the scope of the function
    const int tptt_y_case_1[] = {11, 11, 11, 11};
    const int tptt_x_case_1[] = {6, 7, 8, 9};
    const int tptt_y_case_2[] = {11, 11};
    const int tptt_x_case_2[] = {10, 11};

    int filled = 0; //number of elements

    switch (PastTo_val) {
    case 1:
    {
        tPTT_x = (const int*) tptt_x_case_1;
        tPTT_y = (const int*) tptt_y_case_1;
        filled = 4;
    }
    break;
    case 2:
    {
        tPTT_x = (const int*) tptt_x_case_2;
        tPTT_y = (const int*) tptt_y_case_2;
        filled = 2;
    }
    break;
    }

    Serial.println("Printing array values");
    for(int i=0; i < filled; i++) {
        Serial.println("tPPT_x["+ String(i) +"] = " + String(tPTT_x[i]));
        Serial.println("tPPT_y["+ String(i) +"] = " + String(tPTT_y[i]));
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.