Getting No. of indexes in Arduino Array

Hi everyone,
I'm used this simple sketch of getting no of data in Arduino long array.

long Array1[10] = {321,465564,3214,987546,648454};

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

void loop() {
  Serial.println(Array1[4]);
  Serial.println(sizeof(Array1));
  delay(200);
}

My answers are

648454
40

In this sketch I have added 5 values into this array. But its giving 40 as the output of sizeof() function. how to get no of available indexes (5) as my output.
Thank You.

byte indexCount = sizeof(Array1) / sizeof(Array1[0]);

ie, the total number of bytes in the array divided by the number of bytes in a single element of the array

EDIT : also take note of the advice to let the compiler work out how many array elements to create based on the values provided in the initialisation

1 Like

1. sizeof is an operator and not a function. It gives number of bytes present in a data item.

2. long is a data type that refers to 32-bit (4-byte).
3. You have declared an array with 10-member. So, the sizeof(Array1) operator gives: 10x4 = 40 which you have shown onto Serial Monitor.

4. Re-declare the array without showing the dimension (number of items/members present in the array) and then check that sizeof(Array1) returns 20 (5x4). The Compiler is smart enough to count the number of items present in the Array1.

1 Like

My personal favourite to determine the size of an array:

template <class T, size_t n> size_t arraySize(T const (&)[n]) { return n; }

Example:

int myArray[] = {0, 1, 2, 3, 4};
Serial.println(arraySize(myArray));  // Prints 5.
1 Like

You can't. There is no concept of "unused" or "uninitialized" entries in an array. The ones you don't explicitly initialize are initialized to zero (because global variables are initialized to zero). If you want to keep track of how many entries you have explicitly initialized you will have to do that manually:

long Array1[10] = {321,465564,3214,987546,648454};
size_t Array1Initialized = 5;
1 Like
template <class T, size_t n> size_t arraySize(T const (&)[n]) { return n; }

Easy to use, difficult to understand for a newbie

Please explain what it does and how it does it in English. The equivalent of

1 Like

Does not work -- arraySize is not recognized?

Sure, this function returns the size of an arbitrary array.

How this function works will probably not be of interest to a "newbie", we tend not to explain how digitalRead works either, but I can give it a try.

This function deduces the size of an array from its type. It takes a reference to an array of type T[n], an array with n elements of type T. Both T and n are template parameters, T is used to accept arrays with elements of arbitrary type, n is returned.

1 Like

You have to define it first, see the first snippet in post #5.

1 Like
template <class T, size_t n> size_t arraySize(T const (&)[n]) { return n; }

The above appears to me "bureaucratic"?

1 Like

A new user can go to the Language Reference to find out how to use digitalRead(). It is more difficult to find information on the more esoteric functions and keywords of C++ and how to use them with Arduino.

2 Likes

How to use it indeed, not how it works internally. Perhaps I should have phrased it differently, e.g.,

we tend not to explain the internal workings of digitalRead either

1 Like

You do not need to know the internal workings of digitalRead() to use it. It is sufficient to say that it returns the state of the pin number passed to it

I am afraid that the same cannot be said of your explanation of the template because its parameters are at best obtuse. Your explanation uses terms such a "reference" and "type T" neither of which are used in common parlance

I can say with some certainty that I will not be advocating the of such a template in advice to newbies as clever and as useful as it may be

3 Likes

That was exactly my point.

If this arraySize function would be tucked away in a library, the explanation

would probably suffice.

2 Likes

In fact, that's what I did.

ArraySize.h:

#ifndef ARRAY_SIZE
#define ARRAY_SIZE

template <class T, size_t n>
constexpr size_t arraySize(T const (&)[n]) {
  return n;
}

#endif

Then:

#include "ArraySize.h"

long Array1[] = {321, 465564, 3214, 987546, 648454};
const size_t numElements = arraySize(Array1);

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.print("Number of Elements: ");
  Serial.println(numElements);
}

void loop() {
}

Interestingly, if I copy the code from ArraySize.h directly into the .ino file it causes a compilation error. I've run across that before with templates. I'm guess a conflict with Arduino's auto prototype generation. Moving it to the .h fixes that.

2 Likes

Nice, did you add it to the library index?

You may be interested in this issue:

1 Like

This is getting more and more absurd as time goes on

The query started with a user who wanted to know how to count how many elements there are in an array. He/she was already aware of sizeof() but was confused by what it returned in the case of his/her array

A simple extension of the use of sizeof() and advice to let the compiler determine how many bytes are allocated for the array provided an initial answer the the question

Then we moved on to a solution using a valid but much more convoluted method involving a template

Now we have reached the dizzy heights of putting the template in a .h file and #including it in the sketch

How much more obscure can we make it I wonder ?

Who will be the first person to post a solution using assembly language ?

3 Likes

From the original post

how to get no of available indexes

It's not even clear if he wanted the assigned number of elements in the array or the number of unassigned elements. @johnwasser was the only one who answered the second question.

While the angels are dancing on the head of the pin, perhaps the OP will return and clarify what they wanted.

1 Like

There are plenty of Arduino libraries that use templates but hide that ugliness in their .h file.

1 Like

Or even the number of dimensions which is what I thought upon reading the thread's title.

1 Like