How to get size of allocated memory for string array?

Hello all!
I' need determine the size of string array.
I'm trying like this:

#include <stdio.h>

int count(char *a)
{
    int i = 0;
    int numbersOfBytest = 0
    while(a[i])
    {  
	//What should I do?
	++i
    }
    return numbersOfBytest;
}


int main()
{
    char *massive[] = {"first_word","second_word","third_word"};
    // count number of elements
    int size = count(massive);
    //print number of elements
    printf("Numbers of bytes=%d\n", size);
    return 0;
}

But it's not compiling

Thank you!

I bet a couple of the errors are about the missing loop and setup functions. Here is the reference page for Strings. Strings. When programming a microcontroller with limited memory it may be better to use C-strings (null terminated character arrays).

groundfungus:
When programming a microcontroller with limited memory it may be better to use C-strings (null terminated character arrays).

Which is exactly what he IS using....

Regards,
Ray L.

Sorry, don't know why I thought otherwise.

To find the size of a c string - assuming you don't want to use the built in strlen(), you count characters from position zero until you find the 0x00 character which signals the end of the string.

Rocketar:
Hello all!
I' need determine the size of string array.
I'm trying like this:

#include <stdio.h>

int count(char *a)
{
    int i = 0;
    int numbersOfBytest = 0
    while(a[i])
    { 
//What should I do?
++i
    }
    return numbersOfBytest;
}

int main()
{
    char *massive[] = {"first_word","second_word","third_word"};
    // count number of elements
    int size = count(massive);
    //print number of elements
    printf("Numbers of bytes=%d\n", size);
    return 0;
}



But it's not compiling

Thank you!

If you want to know how long a string is, use strlen(), which will tell you how many characters, NOT counting the terminating null character. If you want to know how much space is actually allocated for a character array (which is not necessarily the length of the string stored in it), use sizeof(), but you can't use that on the argument to a function.

Regards,
Ray L.

That code is will never compile for the Arduinos. It's meant to run on Windows, Linux etc.

This is an Arduino programing forum!

Mark

holmes4:
That code is will never compile for the Arduinos. It's meant to run on Windows, Linux etc.

This is an Arduino programing forum!

Mark

NEVER?? Really?? Change "stdio.h" to "Arduino.h", change "main()" to "setup()", add "loop(){}", and it should compile, and run, just fine.

Regards,
Ray L.

It can't compile:

    int numbersOfBytest = 0

It's nothing to do with the Arduino.

The answer is trivial (strlen).

First post by a new user.

He has taken the trouble to advertise a company:

Auriga is ranked #1 Engineering Services Outsourcing (ESO)

Sounds like spam to me. What do others think?

Missing ; at the end of several lines too.

Simplicity is bliss.

int count(char *a)
{
    return strlen(a));
}

I' need determine the size of string array.

Strange, I read the question differently than everyone else. I thought he was asking the length of the array, not what's contained in the array. I'm not sure which answer he really wanted.

void setup() {
 
  char str[100];
  Serial.begin(9600);
  
  memset(str, 'A', 100);
  str[50] = '\0';
  Serial.print("strlen = ");
  Serial.println(strlen(str));
  Serial.print("Size of string = ");
  Serial.println(sizeof(str));
  
}

void loop() {
}

EDIT: Rereading, I think it was the title of the post that caused me to think as I did.

econjack:
Strange, I read the question differently than everyone else. I thought he was asking the length of the array, not what's contained in the array. I'm not sure which answer he really wanted.

I took it as meaning this too.

If the memory is allocated dynamically, you can read the size stored in the malloc head on AVR's. This method is not portable to other platforms/compilers without testing. For example the Due has 32 bit addressing, compared to the AVR 16-bit pointers, the data may be stored completely different also.

size_t sizeofAlloc( void *v ){ return *((size_t*)v-1); }

void setup() {
  char *data = new char[107];
  
  Serial.begin(9600);
  Serial.print( sizeofAlloc(data), DEC );
  
  delete [] data;
}

void loop() {}

econjack:
Strange, I read the question differently than everyone else. I thought he was asking the length of the array, not what's contained in the array.

I still think he is selling his "Engineering Services Outsourcing (ESO)" and not asking a real question, but I am obviously in the minority here.

Perhaps he wants both.

He can do this easily with a template.

template <class T, size_t N>
void totalSize( T(&Arr)[N], int *result )
{
  result[0] = N;
  for (byte i = 0; i < N; i++)
    result[1] += strlen(Arr[i]);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  char *message[] = {"Hello", "World", "Test"};
  
  int New_result[2];
  totalSize(message, New_result);
  Serial.print(New_result[0]); // number of elements
  Serial.print('\t');
  Serial.println(New_result[1]); // total bytes
}

void loop() {
  // put your main code here, to run repeatedly:

}

Output:

3 14

HazardsMind:
Perhaps he wants both.

He can do this easily with a template.

You should template both parameters so you can make sure you do not overflow the result (your example uses 3 inputs, but has space for 2 results).

Do you mean like this?

template <class T, size_t N, class R>
void totalSize( T(&Arr)[N], R *result )
{
  result[0] = N;
  for (byte i = 0; i < N; i++)
    result[1] += strlen(Arr[i]);
}

I originally had it like this, but decided to just use an int for my test. I don't really see the need for anything bigger than an int, on a platform that has such little SRAM. On a PC, maybe.

If this is not what you meant, can you show me?

HazardsMind:
Do you mean like this?

If this is not what you meant, can you show me?

Ah, I misread your code, I was under the impression that the result array stored the length of each input. But still seems confusing as it will return the number of elements, and the total count of characters in the strings pointed to by each element (which aren't part of the array allocation).

Hi Look here. I guess that this example is not difficult to compile;)

He is advertising a #1 engineering company and comes with this elementary programming question?

I will never hire them ...

that said, discussion is not too bad