I just need to know how to count the entries of an array like this one:

string array[] = {"entry", "entry", "entry", ... , "entry"};

Thanks!

Recommended Answers

All 8 Replies

(sizeof array / sizeof array[ 0 ])

Thanks! now is there a way that I can make a shortcut for that? say...if I wanted to make a function arraycount(array) how would I do that so I don't have to type tha long stuff?
I'm not talking about doing

int arraycount(string array)
{
  int z = (sizeof array / sizeof array[ 0 ]);
  return(z);
}

or some similar code but like to hardwire a new command?
thx

Member Avatar for iamthwee

Thanks! now is there a way that I can make a shortcut for that? say...if I wanted to make a function ...
I'm not talking about doing

int arraycount(string array)
{
  int z = (sizeof array / sizeof array[ 0 ]);
  return(z);
}

What's wrong with your example?

What's wrong with your example?

Try it and see.

Thanks! now is there a way that I can make a shortcut for that? say...if I wanted to make a function arraycount(array) how would I do that so I don't have to type tha long stuff?

Consider a macro.

#define arraycount(x) (sizeof(x)/sizeof(*(x)))
Member Avatar for iamthwee

>Try it and see.

Woah that's strange! I would have never guessed that.

An array of STL strings seems a little strange. Have you considered using STL vectors instead of arrays? They have a built in size() member, no calculation on your part is needed.

I am not familiar with this STL vector you speak of....???

Member Avatar for iamthwee

I am not familiar with this STL vector you speak of....???

google + stl vectors...

God-speed

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.