Greets,

A string array in C++ can be initialized, just like everything else.
With that in mind, how do you count the number of items in the array?
I mean, how many rows?

Let me give you an example using Char arrays (having two dimensional since it's not like string)

char arrChar[][10] = {
"Anagram",
"Book",
"Computer",
"Dimension",
"Epic",
"Fail",
"Gyroscope",
"Hemingway",
"Irish"
};
//Amount of rows initialized:
int Rows = sizeof(arrChar)/10;

Rows is 9, right, now to do this with strings, it's a bit different.
Since a string is null-terminated I approached the problem like so:

string arrString[] = {
"I Like Turtles",
"Icecream",
"Legendary stuff",
"French Fries"
};

int GetStringRows(){
 int rowcount;
 for(int i = 0; !arrString[i].empty(); i++) rowcount++;
 return rowcount
}

This works, but I don't know how reliable this technique is, because after creating another string array, for some reason it adds all strings together and returns the wrong value.

And here is the actual code:

int CountStructure(string *structure){
        int counter = 0;
	for(int i = 0; !structure[i].empty(); i++)counter++;
	return counter;
}
int main(){
        string menu[] = {
        "---------------------------",
        "-        New Game         -",
        "---------------------------",
        };
        string map[] = {
        "---------------------------",
        "-                         -",
        "-                         -",
        "-                         -",
        "-                         -",
        "---------------------------",
        };
	cout<<CountStructure(menu)<<endl;
        cout<<CountStructure(map)<<endl;
	return R_OK;
}

Output:
3
9

Recommended Answers

All 3 Replies

If you have an actual array, you can use the sizeof trick regardless of the type of the array:

int Rows = sizeof arrString / sizeof *arrString;

This divides the size of the array in bytes by the size of the first element of the array in bytes. Since std::string uses dynamic memory for the string contents, the size of each string object will be consistent, just as if you used an array of pointers to char instead of arrays of char for the first example:

const char *arrChar[] = {
  "Anagram",
  "Book",
  "Computer",
  "Dimension",
  "Epic",
  "Fail",
  "Gyroscope",
  "Hemingway",
  "Irish"
};

int Rows = sizeof arrChar / sizeof *arrayChar;

The actual strings have different lengths, but the size of a pointer to char is constant, so the sizeof trick works.

Now, if you're passing the array to a function, this trick no longer works because inside the function the array is no longer an array; it's a pointer. The ideal solution is to pass the size of the array as a second argument. That's a great deal safer and more efficient than the leading alternative, which is to place a sentinel at the end of the array:

const char *arrChar[] = {
  "Anagram",
  "Book",
  "Computer",
  "Dimension",
  "Epic",
  "Fail",
  "Gyroscope",
  "Hemingway",
  "Irish",
  0 // Sentinel
};

int sentinel_size ( const char **array )
{
  int i;

  for ( i = 0; array[i] != 0; i++ )
    ;

  return i;
}

If vectors are in your toolbox, you better use them:

int main(){
std::vector<std::string> vec1;
vec1.push_back("Hello");
vec1.push_back("How");
vec1.push_back("are");
vec1.push_back("you");
f1(vec1);//see definition below
}
void f1(const std::vector<std::string> & vec) //a function taking vector as argument
{
    for(int i=0; i!=vec.size();++i)
        { std::cout<<vec[i];  }//print the vector
}

Ah, thank you for the thorough explanations and examples Narue!
siddhant3s, I don't know much about vectors, do you by any chance have a good link for a tutorial on them?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.