How can I know the number of index of a char

Good evening,

A function return me a array

splitedString[i]

which look like

spliedString[0]="2";
splitedString[1]="4";
splitedString[2]="5";
splitedString[3]="10";
splitedString[4]="11";
splitedString[5]="12";

In my exemple there is 6 index; 0,1,2,3,4,5.

But it can have more or less.

Later, in my code I need to read splitedSTring and I am using a for loop

for(int i=0; i<???; i++)
{
Serial.println(splitedString[i]);
}

I know sizeof(splitedString)
which will return an int of a value of 9, because, 2,4,5,1,0,1,1,1,2

But it does not count the number of index which can change depending of a request.

Is there an easy way to know the number of index to the “???” in my for loop?

Many thank

But it does not count the number of index which can change depending of a request.

Is there an easy way to know the number of index to the "???" in my for loop?

Yes.

A function return me a array

It knew how many elements in wrote in the array. It must tell you.

Or, it must ensure that there is a NULL in the next position. Then, you'd iterate until you found the NULL, rather than a fixed number of times.

Since a function cannot return a local array, the calling function must know the size of the array; either it’s defined in the calling function or it’s a global. You can use the old macro to find its size:

#define ELEMENTCOUNT(x) (sizeof(s) / sizeof(x[0]))

// ...more code...

  for(int i=0; i<ELEMENTCOUNT(splitedString); i++)
  {
     Serial.println(splitedString[i]);
  }

There are typically 2 parts to this. In C, a string is an array of characters.

You can either allocate the array dynamically (via a function like malloc(), like this:

char *aString = malloc(30);

… use your string

//Release your string from the heap when you are done with it.
free(aString);

You can’t use sizeof() on a buffer that you create using malloc. You need to remember how big it is.

Or you can define a string at compile time, like this:

char *aString[30];

Such an array is always 30 bytes long, and you CAN use sizeof to get the size (capacity) of the string:

int stringSize = sizeof(aString);

The second part of the puzzle is many characters are currently stored in the string.

C strings use a null terminating character by convention. The standard C library includes a number of functions for working with C strings, including strlen(). That function searches a C string looking for a null, and returns the number of bytes found before the null.

Note that C strings are not widely used for parsing human readable text any more because they can’t store non-roman character sets, special symbols, etc.

The burden is on you to not store more data in a string array than the space allocated for it. If you do write more data than the size of the buffer, you cause a buffer overrun and bad things may happen. (App crashes, unexpected results, etc.)

Are you trying to figure out the size of your string buffer, or the length of the contents of the string, up to the terminating null byte?

pierrot10:
Good evening,

A function return me a array

splitedString[i]

which look like

spliedString[0]="2";

splitedString[1]=“4”;
splitedString[2]=“5”;
splitedString[3]=“10”;
splitedString[4]=“11”;
splitedString[5]=“12”;



In my exemple there is 6 index; 0,1,2,3,4,5.

But it can have more or less.

Later, in my code I need to read splitedSTring and I am using a for loop


for(int i=0; i<???; i++)
{
Serial.println(splitedString[i]);
}




I know s`izeof(splitedString)`
which will return an int of a value of 9, because, 2,4,5,1,0,1,1,1,2

But it does not count the number of index which can change depending of a request.

Is there an easy way to know the number of index to the "???" in my for loop?

Many thank

Or you can define a string at compile time, like this:

That is a an array of pointers to strings, not a string. Lose the * to define a char array. Null terminate the array to make it a string.

Because of the small amount of SRAM available in an Arduino I would define a global array with enough space for the biggest string of characters I would need.

Then my code would just need to keep track of how many characters had been received in order to iterate over the latest string.

Note I am talking about strings with a small s - char arrays terminated with a 0.

…R