Array multiple element selection ?

Hello,
Is it.possible to.access several elements at the same time? e.g. think you want to move all elements in an array to create a FIFO queue and enter the last value in the last position. Can you do this without writing a loop or without reading and writing all elements one by one?

Thanks

you could do some calculations then use memcpy() to bump your array contents around.
One call to execute a hidden asm block of code.

federicof89:
Hello,
Is it.possible to.access several elements at the same time? e.g. think you want to move all elements in an array to create a FIFO queue and enter the last value in the last position. Can you do this without writing a loop or without reading and writing all elements one by one?

Thanks

There is no C or C++ basic instruction that will select a subpart of an array to perform some operation on it but as @lastchancename mentions, an array is just a bunch of adjacent bytes in memory and thus with proper calculation you can use the memcpy() function to take a number of bytes from area1 in memory to area2 (just ensure both areas do not overlap)

for example, if you run this and open the Serial monitor at 115200 bauds

const uint8_t nbElements = 10;
long array1[nbElements] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
long array2[nbElements] = {0}; // only 0 in the array

void setup() {
  Serial.begin(115200);
  Serial.print(F("BEFORE: "));
  for (uint8_t i = 0; i < nbElements; i++) {
    Serial.print(array2[i]);
    Serial.write('\t');
  }
  Serial.println();
  // move 4 items from array1 to array2, so 4 x number of bytes required for a long, starting from the 4th position (index starts at 0 so index 3 is the 4th element)
  memcpy(array2 + 3, array1 + 3, 4 * sizeof(typeof(array1[0]))); // memcpy(destination, source, num ) http://www.cplusplus.com/reference/cstring/memcpy/
  Serial.print(F("AFTER : "));
  for (uint8_t i = 0; i < nbElements; i++) {
    Serial.print(array2[i]);
    Serial.write('\t');
  }
  Serial.println();
}

void loop() {}

you'll see

[color=purple]
BEFORE: 0	0	0	0	0	 0	 0	 0	0	0	
AFTER : 0	0	0	[color=red]8	16	32	64[/color]	0	0	0	
[/color]

we moved 4 elements from array1 to array2, starting at index 3 (4th position)with the command  memcpy(array2 + 3, array1 + 3, 4 * sizeof(typeof(array1[0])));

arrays are typed, here a long, and the name of the array is a pointer to its type, so here array1 is a pointer to along (long*).

When you add to a pointer, the compiler knows you want to skip to the next data that's pointed, so when I do array1 + 3 it knows I want to skip 3 long so 12 bytes. (same goes for array2+3)

the function then takes the number of bytes you want to move, so this time you really need to count bytes. As I want to copy 4 long, I need to multiply the number of bytes in a a long by 4. I could have done 4 * sizeof(long) for that, but 4 * sizeof(typeof(array1[0])) is more modular, if you change the type of the array, the code will adapt to whatever type you chose.

hope this helps

e.g. think you want to move all elements in an array to create a FIFO queue and enter the last value in the last position

You are approaching the problem the wrong way. You don't need to select or move any of the array elements to manage a FIFO queue. Keep track of the array index of the head and tail of the queue and use those to access the required data element to either add or remove an element in the queue.

See circular/ring buffer. Creating a Circular Buffer in C and C++ - Embedded Artistry