Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by Liam78 Aug 31, 2005 at 7:06 PM 13 replies 78.5k views
Original Post
Liam78
Liam78
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
SiCrane
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 *).

For dynamic arrays, you either need to store the number yourself or use a class like std::vector.
nilkn
nilkn
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.
tychon
tychon
In addition to the MSDN, I'd recommend taking a look at cppreference.com.
Liam78
Liam78
thankyou for the quick responses people,

appreciated

Li
mozie
mozie
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.
Zaris
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.
perfectly_dark
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.
nprz
nprz
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.
Oluseyi
Oluseyi
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.
SiCrane
SiCrane
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().
benryves
benryves
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).
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
Zahlman
Zahlman
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.
Iftah
Iftah
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.