Arduino push a char into char array right shift the entire array but all char change to the same one

I push a char into a char array in Arduino. Here is the code:

#define q_length 10
char * queue[q_length] = {"10","9","8","7","6","5","4","3","2","1"};
void setup() {
  Serial.begin(9600);
}
int in = 0;
void loop() {
  char buf[60];
  strcpy(buf, "--");
  char ii[20];
  itoa(in, ii, 10);
  strcat(buf, ii);

  for (byte i = q_length-1; i > 0; i--){
    queue[i] = queue[i - 1];
  }
  queue[0] = buf;
  in ++;

  for (byte i = 0; i < q_length; i++) {
    Serial.print(i);
    Serial.print(":");
    Serial.println(queue[i]);
  }
  Serial.println("==========");
  delay(1000);

}

and output

0:--0
1:10
2:9
3:8
4:7
5:6
6:5
7:4
8:3
9:2
==========
0:--1
1:--1
2:10
3:9
4:8
5:7
6:6
7:5
8:4
9:3
==========
0:--2
1:--2
2:--2
3:10
4:9
5:8
6:7
7:6
8:5
9:4

As you can see, every time I push a new one the other char I just pushed change became the new one.

I want to push the new char into the char array index 0, right shift the entire array, and just change the index 0 char. Not all of them.

You assign the pointer to that location… you don’t copy the content over, so…

I try strcpy

for (byte i = q_length - 1; i > 0; i--) {
    strcpy(queue[i], queue[i - 1]);
}
strcpy(queue[0], buf);

output:

could you tell me how to do please.

Strcpy would require memory to store the string at the destination… you only declared an array of pointers

If you had a 2D array that could work (as long as you don’t overflow the memory allocated for each string)

1 Like

This is your code with some changers and deletions.

I have no idea what you are trying to do, but this makes nice output

# define q_lenth 10

// bzzzt! THX @J-M-L char queue[q_lenth] = {'10','9','8','7','6','5','4','3','2','1'};

char queue[q_lenth] = {'9','8','7','6','5','4','3','2','1','0',};

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

void loop() {

  unsigned char temp = queue[q_lenth - 1];

  for (byte i = q_lenth-1; i > 0; i--){
    queue[i] = queue[i - 1];
  }

  queue[0] = temp;



  for (byte i = 0; i < q_lenth; i++) {
    Serial.print(i);
    Serial.print(":");
    Serial.println(queue[i]);
  }
  Serial.println("==========");
  delay(1000);
}

Put your finger on the code and see how it works. Put your finger on your code and see why it doesn't.

Look at the differences between the two. Tray to figure out what the differences mean.

a7

'10' does not fit in a char....

No, no it doesn't. I'll leave it to later to see why I still got OK output.

Replace '10' with '0'.

a7

thanks for your replay.I'm trying to make a queue.I got too many data from a sensor in a moment. then sent the data through a lora by ttl. interval 500ms. so I need a queue send them one by one. I read you code. It juse makes a nice output.

It's easier to use logic than to do all that shuffling.

Look into "ring buffers" or "circular buffers".

Easier and if it is ever important, faster.

a7

got it. thx.

For future reference, this is a recipe for disaster. buf needs to be a nul-terminated character array. Either use memset() to initialise it or use

char buf[60];
buf[0] = '\0';
strcpy(buf, "--");

no...

strcat would but strcpy will happily overwrite whatever is there and write the trailing null char

Time to go to bed for me.

coffee or a walk outside works too :wink:

I'm having coffee and sitting outside (having a smoke).

I'm sure that was the smoke then... bad habit !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.