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

Started by
13 comments, last by Iftah 18 years, 7 months ago
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
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.
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.
In addition to the MSDN, I'd recommend taking a look at cppreference.com.
thankyou for the quick responses people,

appreciated

Li
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.
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.
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.
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.
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.

This topic is closed to new replies.

Advertisement