How to check if array is empty?

Hello guys,

I can't understand how Arduino can't provide methods for easy use of arrays, like length of array or check if array is empty. Seems pretty basic to me.

I have this:

String getstrValue ( const String oldString, const char* key, const String parents[] = {} ) {
   int sizeParents = sizeof(parents) / sizeof(parents[0]);
   Serial.println(parents[0]);
   return String();
}

void loop(){
  String time =  getstrValue(strData, "time");  
  String temperature = getstrValue(strData, "0", parents);
}

The parents[0] cause error:

Exception (28):
epc1=0x40205d44 epc2=0x00000000 epc3=0x00000000 excvaddr=0x0000000b depc=0x00000000
...

sizeParents is always == to 0.
I need to use the optional argument. What I did wrong?
I'm on Esp8266.

sounds like you may be thinking of a char array used to store c-strings, not String.

since all c-strings are by convention terminated by a NUL, '\0', the c-string is NUL (i.e. empty) if the first array element is NUL

You should start by describing what you really want to do, not how you think it should be done. The code you posted is a jumbled-up mess of different data types and classes.

1 Like

what would an empty array look like?

I made change:

String getstrValue(const String oldString, const char* key, const char* parents[] = nullptr) {
const char* parents[] = {"hourly", "temperature_2m", nullptr};
String temperature = getstrValue(strData, "0", parents);

Same error.

Actually i like the default value: {}

ditto

2 Likes

I think that below will be the pointer.

The argument parents is a pointer; the size of a pointer is fixed (2 bytes on AVR, 4 bytes on 32 bit architectures).
The size of a String object (e.g. parents[0]) is 6 (on AVR, not sure about 32 bit, it is probably 8).

Divide the two and the result is 0.

Like I said I just would like to pass an optional array argument, empty by default, then able to check if that's empty or not.

Anyway with the modified code I'm able to check it:

if(parents == nullptr){  // i was not able to do parents == {}
    Serial.println("null");
  }
else{
Serial.println(parents[0]);
}

No error anymore.
Thanks

The Arduino runs standard C/C++. Use any methods you like that are available in that programming standard.

Actually i like the default value: {}

This is utterly meaningless without defining the context.

2 Likes

I think about that then try: myArray.empty() for C++ and got error.

And you are surprised?

:rofl: yes, I forgot my C++.

these are 2 different things

the first is a typical c-string (char array). setting it to null implies that nothing in memory is allocated where
char * parents [10] = {};
means there's a 10 byte array with all elements set to 0 (i.e. null)

sorry, the above is wrong, parents an array of char arrays, but nothing it allocated

the 2nd suggests an array of c-string, an array of arrays. with each c-string array allocated the # of bytes to store the specified c-string (e.g. "hourly")

you can pass a ptr to an array of arrays along with value indicating the # of arrays.

you can then check both is the ptr to the array, e.g. parents [1] is nul (is there any memory allocated, and if there is, does it have content, is the 1st byte not nul, parents [1][0] == nul

1 Like

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