slice cells from array

hello,

does anybody know if there's an easy way to slice cells from an array, maybe similar to something like this:

arrayName.slice(3);

Thanks! Daan.

What, precisely does, or would, slice do the array? What does, or would, it return? What does, or would, it take as an input?

Unlike scripting languages like perl, python, haskell, the C language is quite primitive when it comes to arrays. There are no operators to figure out their size, find elements, work with smaller portions like slices, etc. There’s just a row of contiguous elements. You have to do all of the manipulation yourself.

int jenny[7] = { 8, 6, 7, 5, 3, 0, 9 };
Serial.begin(9600);
for (i = 0; i < 7; i++) {
    Serial.print(jenny[i], DEC);
    if (i == 2) Serial.print('-', BYTE);
}
Serial.println();

Once you’re comfortable with pointers, there are some tricks you can do with them, but for most purposes, learn how to do what you want without pointers first, then apply them to generalize a routine.

@paulS

What, precisely does, or would, slice do the array? What does, or would, it return? What does, or would, it take as an input?

I'd like to remove an arbitrary cell from a 1d array and return the array without this cell... so basically glue the two parts back together.

@halley

Thanks,

so, would an appropriate way of doing this be something like a for() which:

  • starts to copy stuff into a new array
  • skips the cell which is to be sliced
  • continues copying
  • return this

or is there a faster way?

Thanks!

If you’re comfortable with pointers, you can copy the before part and the after part to a new array with just two calls to memcpy.

AWOL, hah, good catch. Hate blowing a joke with a typo like that. Fixed now.