You are on page 1of 1

I found the solution, which seems to be much more simple than I thought...

When I started working on this I thought about string variables as to a dynamic size variable which
would take much or less memory based on how much is needed by the content. So if the content
would have been "this" the variable size would have been 4 and if content would have been "a very
long text here" the size would have been 22.
But I was totally wrong!
In C++ any simple type has it's own maximum size which is defaulted. For string it is 8 bytes.
When you set some content in the string what actually changes is only the length of the content,
not the size of the variable. So, even if I set a string of 50 chars, the size of that string would still be
8 bytes while the length of the content would be 50 chars.
The solution then is just as simple as:
Code:

string array[]={"any", "strings", "you", "want", "here"};


unsigned int sz = sizeof(array)/sizeof(string);
In this case, the total size of array is 40bytes which divided by 8 ( sizeof(string) ) = 5 -> the
number of items contained in the array.
So, I'm sorry for having waisted your time, I should have checked this before.
Thanks to anyone for help.

You might also like