get used/Initialised elements of array

I would like to get the used elements of any kind of array. I am not quite sure if that is possible at all or if it might can be archived with some work-arounds (initialising an empty value, like '\0' and then check for those or having an external counter, whatsoever ).
Thats what I got so far, I would like to get the initialised values:

#define len(arg) ((unsigned int) (sizeof (arg) / sizeof (arg [0])))// number of items in an array
void setup() {
  Serial.begin(9600); // init port
  while (!Serial); // wait, till port available
  Serial.println("-");

  int Array [10]; //array decleration
  Array[0] = 1;  //init array
  Array[1] = 5;  //init array
  
  Serial.println(len(Array));//length of array
  //returns: 10 
  Serial.println(sizeof(Array));//number of bytes in array
  //returns: 20

  //question:
  Serial.println(lenEl(Array));//"used/initilised" elements in array
  //should return: 2

}
void loop() {
}

I am happy for any kind of input.

It’s not possible, you have to use a counter,

Or initialize the array with 0’s (with memset) and then break a for loop when it sees a 0

Example

int Array [10]; //array decleration
memset(Array, 0, sizeof(Array));

Array[0] = 1;  //init array
Array[1] = 5;  //init array

size_t lastItem = 0;
for (size_t i = 0; i < sizeof(Array)/sizeof(Array[0]); i++)
{
	if (Array[i] == 0)
	{
		lastItem = i-1;
		break;
	}
}
Serial.println(lastItem); // should print 1, the index of the last "used" element

Whether you explicitly set the value of an array element to a value or not it will always contain something which means that you cannot simply determine whether an element has been used or not. Also, if an array element might contain any valid value, say 0-255 for an array of bytes, then you cannot detect used or unused elements based on the value that they contain

A couple of suggestions
If all you need to do is to count the number of used elements then increment a counter variable each time that an array element is changed

If you want to know which elements have been used and/or the value that they contain, then use two arrays with the same number of elements. One to hold the values and the second to hold a flag indicating that the corresponding element has been used. The two array could be put in a struct to make handling them easier

Another possibility is to use the bits of a variable or variables as the used/unused flags, which would save memory

It would be helpful to know exactly why you want to do this as other, more elegant solutions may be applicable

Thank you for your reply. That helped a lot already.
I am trying to check a certain array to a certain condition and then appending it to another array. As far as I am concerned there are no array lists, like in java for example.
like the following:

int arr[] = {1, 2, 3, 4, 5};
  int oddNumbers[5]; //number of odd numbers is maximal the size of arr[]
  //get all odd numbers and store it in a odd list
  for (int i = 0; i < 5; i++) {
    if (arr[i] % 2) {//get all odd numbers
      Serial.println(arr[i]);
      //something like that
      //oddNumbers.append(arr[i]); //append to oddNumbers
     
    }
  }
  
  //printout the odd numbers after storing ----- DOESN'T WORK
  for (int k = 0; k < lenEl( oddNumbers); k++){
    Serial.println(oddNumbers[k]);
  }

I am heavily working with python, so I usually use lists to archive that.
The size of the array can be assumed. Is there a better way to print just the “used” part of “oddNumbers”, then just archiving that with a counter?

int oddNumbers[4]; //number of odd numbers is maximal the size of arr[]

Fix this before you go any further because the arr array has 5 elements

Thanks!

OK, so what I find it really weird that there is a function strlen() to do exactly that, but just for a char arrays. Is there no different way for a int array for example?

Well in C++ there are containers such as std::vector, which has methods like push_back() that is the equivalent to python's append(). I don't encourage you to use this, but if you do then make sure to use reserve() as to avoid memory reallocation.

I suggest to use an array and a counter to keep track of the last element

strlen is just counting characters until it finds a 0 (basically the same as my example code above)

Alright thanks, will stick to the counter. No need of getting a splitted memory, because of allocations ...

Wise :slight_smile:

so what I find it really weird that there is a function strlen() to do exactly that, but just for a char arrays. Is there no different way for a int array for example?

Try this

char charArray1[4];
char charArray2[] = {"1234"}; //5 bytes of storage needed
byte intArray[10];
long longArray[4];
float floatArray[20];

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  Serial.print(sizeof(charArray1));
  Serial.print("\t");
  Serial.println(sizeof(charArray1) / sizeof(charArray1[0]));
  Serial.print(sizeof(charArray2));
  Serial.print("\t");
  Serial.println(sizeof(charArray2) / sizeof(charArray2[0]));
  Serial.print(sizeof(intArray));
  Serial.print("\t");
  Serial.println(sizeof(intArray) / sizeof(intArray[0]));
  Serial.print(sizeof(longArray));
  Serial.print("\t");
  Serial.println(sizeof(longArray) / sizeof(longArray[0]));
  Serial.print(sizeof(floatArray));
  Serial.print("\t");
  Serial.println(sizeof(floatArray) / sizeof(floatArray[0]));
}

void loop()
{
}

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