Sign in to follow this  

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

This topic is 4473 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
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[i], 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
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
end() is like any iterator into a vector, it can be invalidated through the use of member functions on the vector. In a nutshell, anything that adds or deletes elements to the vector will invalidate end().

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok, this thred hasn't been active for some time. I found the info on how to get the length of an array very useful (and simple). But when I tested it I found out that the returned number of elements is smaller for 1 than the acctual length of an array.

Could someone pls explain why does this happen cuz I don't find it logical. I tested this on arrays of characters (my task was to write my own string copy funtion).

Share this post


Link to post
Share on other sites
I'm assuming that the number of elements returned is the upper bound of the array in that case - it returns the index of the last element.
So,
char *animals[] = {"cat", "dog", "mouse", "elephant, "rhino", "snake", "fish"};
...would have a returned count of 6, as element #6 is the last one (0="cat", 1="dog", 2="mouse"... and so on).

Share this post


Link to post
Share on other sites
No, the number of elements (as determined for example by a sizeof trick) is the number of elements. Indices range from 0 to n-1 inclusive. It's not that hard to remember and it really, really is handy behaviour.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
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 *).



a more generic approch is using a macro (yuck, i know)
(works only for static allocated)

#define ELEMENT_NUM(_array) (sizeof(_array)/sizeof(_array[0]))


Iftah.

Share this post


Link to post
Share on other sites

This topic is 4473 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this