Jump to content
  • Advertisement
Sign in to follow this  
Liam78

how to find the length of an array in c/c++ ?

This topic is 4662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all I was wondering if there is a way to find out the length of an array in c or c++ for example: char *animals[] = {"cat", "dog", "mouse", "elephant, "rhino", "snake", "fish"}; is there a function that would return to me the number of animals that are stored in this array? In flash for example it would be something like: int num_of_animals = animals.length(); ======================================================== ALso is there a way i can find all the functions that my complier has? for example: printf() scanf() etc,,, will it be in a help file?! i am using bloodched Dev-C++ thx Li

Share this post


Link to post
Share on other sites
Advertisement
For a statically allocated array you can use number_of_elements = sizeof(array)/sizeof(type_of_array); In your example: number_of_animals = sizeof(animals)/sizeof(char *).

For dynamic arrays, you either need to store the number yourself or use a class like std::vector.

Share this post


Link to post
Share on other sites
You can, however, use the SC++L containers, such as std::vector and std::list. [smile]

Quote:

ALso is there a way i can find all the functions that my complier has? for example:
printf()
scanf()
etc,,, will it be in a help file?! i am using bloodched Dev-C++


It's very unlikely the compiler comes with such a list (I've never used bloodshed, so I'm not totally certain, but fairly confident).

There is, however, a plethora of online sources, such as MSDN.

Share this post


Link to post
Share on other sites
Here is a little sample of how you could use the vector like SiCrane mentioned.


#include<vector>
#include<string>

int main()
{
std::vector< std::string > Animals;
Animals.push_back("cat");
Animals.push_back("dog");
Animals.push_back("mouse");
Animals.push_back("elephant");
Animals.push_back("rhino");
Animals.push_back("snake");
Animals.push_back("fish");

int sz = Animals.size();

std::cout << sz << std::endl;

std::vector< std::string >::iterator b = Animals.begin();
std::vector< std::string >::iterator e = Animals.end();

while(b != e)
{
std::string name = *b;
std::cout << name << std::endl;
++b;
}

system("pause");
return 0;
}



You can also treat the vector<> like an array with Animals, but I feel like you loose some of the functionality built into vector working like that.

Share this post


Link to post
Share on other sites
if you assign the iterators like that does it change with the vector; If I put iter = .end() would it still point to the end when I add items.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zaris
if you assign the iterators like that does it change with the vector; If I put iter = .end() would it still point to the end when I add items.


I think .end() will ALWAYS point to the last element, no matter how you added it.

Share this post


Link to post
Share on other sites
Quote:
Original post by perfectly_dark
Quote:
Original post by Zaris
if you assign the iterators like that does it change with the vector; If I put iter = .end() would it still point to the end when I add items.


I think .end() will ALWAYS point to the last element, no matter how you added it.


It usually points to 1 past the last element. Otherwise when you loop until end() you will stop before actually using the last element.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zaris
if you assign the iterators like that does it change with the vector; If I put iter = .end() would it still point to the end when I add items.

No, it won't.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!