Separate array and save to strings for later use

Asked

Viewed 401 times

0

I have a bond for* what account of 1 to 8 and keeps in a array calling for str, but I would like that when the variable str to reach 8 is stored the value of str[i] in the array str_line[b];

Example:

  1. The noose for initiating
  2. It keeps at each cycle the value in str[i]
  3. When i=8 stores everything that has been accumulated so far on str[i] and keeps in str_line[b]
  4. Increases the b
  5. Zera the i and starts all over again
  6. When the b is in 5, for the loop for

The program would do this during its execution:

str[1] = 1
str[2] = 11
str[3] = 111
str[4] = 1111
str[5] = 11111
str[6] = 111111
str[7] = 1111111
str[8] = 11111111

Guarda tudo que estiver em `str[8]` em `str_linha[1]`;

Incrementa o `b*`

E repete isso até o laço FOR fizer isso 5 vezes.

I’m having trouble transferring the value of str[8] for str_line[1], how can I do?

My code so far:

char str[9];
char str_line[6];
int i;
int b;

for(i = 0; i < 8; i++)
{
   str[i] = '0' + i;
   Serial.println(str);


   if(i = 8)
   {
       //Aqui guardaria o valor em str_line.

       //Zera o ponteiro e começa a contagem do FOR desde o inicio
       i = 0;

   elseif(b = 5)
   {
       //Para o laço FOR
   }
 }

}
  • Did the answer solve your problem? Do you think you can accept it? If you don’t know how you do it, check out [tour]. This would help a lot to indicate that the solution was useful to you and to give an indication that there was a satisfactory solution. You can also vote on any question or answer you find useful on the entire site.

1 answer

1

I think this code is weird, I’d do it another way, but come on:

char str[9];
char str_line[6][9];
for (int i = 0, b = 0; i < 8; i++) {
   str[i] = '0' + i;
   Serial.println(str);
   if (i == 8) {
       str[8] = '\0';
       memcpy(str_line[b], str, 9);
       i = 0;
       if (++b == 5) break;
   }
}

I put in the Github for future reference.

I haven’t tested, but that’s basically it. There may be other undetected errors. I don’t know exactly what the expected result is.

  • Hello bigown, Thank you. Nice, but how to save the value of str[8] in another array, for example str_line[b] ?

  • Okay. I thought the problem was just the flow, the syntax that’s all wrong.

  • Thank you, I tested the code here and tried to print str_line[3], but not saving anything, what will be ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.