multidimensional char Array

hi guys,

what i get from my moblie app is one string for each scene which the user can change in the app.
theses Scene strings look like this <Z1_11_12_13_1_2_5_7_8_9_10 ....13>
Z is the information that its a Scene, and the number behind Z is the SceneNumber...

I want to seperate it now into the 20 values behind the Z1_ into 20 char arrays.
and because i got 40 Scenes to store each one carrying 20 diffrent char arrays. i tought a multidimensional char array would do it...

but somehow my while in while code is wrong.. could you have a look at it please ?

char Szenen[40][20];         // 40 Szenen  20 Devices=  800 char Arrays* 2 = max 1600 byte
int index;
int index2;
char inData[65];
    if(inData[1] == 'Z') {             // <Z1_13_2_14_1_3_> Scene
      completed = false;
      char counter[2];
      if(inData[3] == '_') {              // Scenennumber < 10
        counter[0] = inData[2];
        index = 4;
      }
      if(inData[4] == '_') {              // Szenennumber > 10
        counter[0] = inData[2];
        counter[1] = inData[3];
        index = 5;
      }
      //Szene wurde erkannt jetzt müssen die Char Arrays zu der Szene sortiert gespeichert werden

      int szene = atoi(counter);
      Serial.print(szene);
      delay(1000);
      int programm = 0;
      index2 =0;

until here its all good... now something is wrong:

      while (inData[index] != '>') {
        while (inData[index] != '_') {
        counter[index2] = inData[index];
        index++;
        index2++;
        }
        Szenen[szene][programm] = counter;
        programm++;              
        index++;
        index2= 0;
      }

Why do you want to save the data as strings, rather than bytes?

    if(inData[1] == 'Z') {             // <Z1_13_2_14_1_3_> Scene

You need to show us how inData is populated, and prove that it looks like you say it does.

now something is wrong:

Only you have the complete code.
Only you know what the code actually does.
Only you have any idea that something is wrong.
Only you have any idea what that something is.

Only you can fix the problem.

      int szene = atoi(counter);
  1. You did not make 'counter' long enough to hold the required null terminator
  2. You did not add the required null terminator.
        Szenen[szene][programm] = counter;
  1. You should have gotten an error trying to store the address of a character array into a character.
  2. You can't store two characters into a single character.
  3. If you intended to convert the two characters into a number first, see 1 and 2.

PaulS:
Why do you want to save the data as strings, rather than bytes?

    if(inData[1] == 'Z') {             // <Z1_13_2_14_1_3_> Scene

You need to show us how inData is populated, and prove that it looks like you say it does.
Only you have the complete code.
Only you know what the code actually does.
Only you have any idea that something is wrong.
Only you have any idea what that something is.

Only you can fix the problem.

it is the whole code of the function ...

it is the whole code of the function ...

That does not tell us what the code actually does.
It does not tell us what you expect the code to do.
It does not tell us how inData is populated.

Good luck solving your problem yourself.

PaulS:
Why do you want to save the data as strings, rather than bytes?

    if(inData[1] == 'Z') {             // <Z1_13_2_14_1_3_> Scene

You need to show us how inData is populated, and prove that it looks like you say it does.

i wanna safe them as strings cauz the arduino is just getting data from my App and from the lightorgan and sends it to all other devices... its for my smarthome Light... and the other devices wanna see strings :slight_smile:

as i said the string looks like this "<Z1_13_2_14_1_3_>\0 "

now something is wrong

and that something is . . . ?

i wanna safe them as strings cauz the arduino is just getting data from my App and from the lightorgan and sends it to all other devices... its for my smarthome Light... and the other devices wanna see strings

So, you want to use 3 bytes to save "14" as a string, instead of one byte to store it as 14. I see.

Actually, no, I don't.

johnwasser:

      int szene = atoi(counter);
  1. You did not make 'counter' long enough to hold the required null terminator
  2. You did not add the required null terminator.
        Szenen[szene][programm] = counter;
  1. You should have gotten an error trying to store the address of a character array into a character.
  2. You can't store two characters into a single character.
  3. If you intended to convert the two characters into a number first, see 1 and 2.

Thank you but it seems not to be the problem :frowning:

BuffaloChill:
Thank you but it seems not to be the problem :frowning:

C and C++ can be like that - all seems OK, then WHAM!

PaulS:
So, you want to use 3 bytes to save "14" as a string, instead of one byte to store it as 14. I see.

Actually, no, I don't.

...you are right paul. But this doesnt make my while loop do what its supposed to :slight_smile:

But this doesnt make my while loop do what its supposed to

The while loop IS doing what it is supposed to do. The issue might be that it is not doing what you expect it to do. In that case, your expectations are wrong.

As to how to fix your expectations, you STILL need to tell us what the code ACTUALLY does, and what you EXPECT the code to do. Then, we can tell you why your expectations are wrong, and how to make the code do what you expect it to do.

the end of a String is ...13>
now look at my while while loop it never ends right ?

now look at my while while loop it never ends right ?

Is that a statement or a question?

I can't see all of your code. I can not tell what is in inData when the outer while loop starts. I can not tell what is in inData[index] each time the loop iterates.

        counter[index2] = inData[index];
        index++;
        index2++;
        }
        Szenen[szene][programm] = counter;

The 2D array, Szenen, is an array of chars. You can not store an array (counter) in a char variable/array element. That has been pointed out, but you've not demonstrated what change(s) you have made to deal with this error.

i thought this is like a Table with X:Y where i can store arrays in each zell where the two pointer Szene Programm point to ?
or can i just store ONE byte where the two pointer point to ?

BuffaloChill:
i thought this is like a Table with X:Y where i can store arrays in each zell where the two pointer Szene Programm point to ?
or can i just store ONE byte where the two pointer point to ?

What two pointers?

char Szenen[40][20];         // 40 Szenen  20 Devices=  800 char Arrays* 2 = max 1600 byte

If you made Szenen a 2D array of pointers, (char *Szenen[40][20]), you could then make the pointers point wherever you wanted. But, really, don't go there.

hmm ... it works now.. but i cant store it the way i want

      index2 =0;
      programm =0;

      while (ended == false) {
        if (inData[index] != '_'){
        counter[index2] = inData[index]; 
        index++;
        index2++;
        }
        if (inData[index] == '>'){
            ended = true;
        }
        if (inData[index] == '_'){
        counter[index2] = '\0';
        int temp = atoi(counter);
        Serial.print(temp);
        Serial.print('_');
        Szenen[szene][programm] = temp;
        programm++; 
        index++; 
        index2= 0;
        }
      }  
      ended = false;

i got 40 [Szene] and each of this 40 arrays consist for 20 values [programm] each is now integer ty for the hint paul ! its better to store it this way.. but
i want to be able to set a pointer on the [Szene] and a second pointer on the [programm]
how do I have to store it then ?

i want to be able to set a pointer on the [Szene] and a second pointer on the [programm]

I don't understand what this means. Arrays and pointers are very closely linked. A 2D array is really just an array of arrays. So accessing the data in the rows for a column, using szene, or the columns for a row, using programm is easy. I don't understand what you are trying to do with the data after this snippet parses and stores it.

Perhaps if you were to actually post ALL of your code, we'd have more useful information to offer.

Szenen[szene][programm];

i want to be able to set a pointer on the [Szene] and a second pointer on the [programm]
how do I have to store it then ?

Something like this?

const int SZENEN = 40;
const int STEPS = 20; 
char Szenen[SZENEN][STEPS];


for (int szene=0; szene < SZENEN; szene++) {
    char *currentSzene = Szenen[szene];
    for (int step = 0; step <= STEPS; step++) {
        char *p_currentStep = &currentSzene[step];
        char currentStep = *p_currentStep;

        // You can skip the whole "pointer to the current step" part by just treating the
        // currentSzene pointer as an array:  char currentStep = currentSzene[step];
    }
}

You can usually treat an array as a pointer and a pointer as an array.