Advertisement

Arrays

Started by May 28, 2000 04:35 PM
6 comments, last by RedKnite 24 years, 7 months ago
Is there anyway to determin the number of elements in an array? For some reason i cant do this. thanks.
quote: Original post by RedKnite

Is there anyway to determin the number of elements in an array? For some reason i cant do this. thanks.


Depends on the language and the type of array. If it is just the basic array in C/C++, then you have to keep track of the size yourself. If it is string (ie null terminated character array) then you can use strlen(string). If it is vector (ie STL) the it has a size method that you can use.


Andrew
Advertisement
Hello!

Well, from my point of view, it depends on what you define as an element or not. That is, if you (in C/C++) create an array like this :
int map[5][5] =
{

{1, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}

};


It can have 25 elements, or 1 element. It depends on what kind of element you want to count.
If its the non-NULLs, or non-zero, or something, you could do a for-loop (in this case) with something like this :

int number_of_elements = 0;

for (int line = 0; line < 5; line++)
{

for (int row = 0; row < 5; row++)
{

// 0 or NULL, or whatever you count as non-item
if (map[line][row] != 0)
number_of_elements++;

}

}


I believe you could do something like this also to count the number of elements (or chars) in an array, and look for "NULL".

Well, I hope I was clear (if not, let me know! ). Cya,
-RoTTer
Ill give that a try, the thing is i have an array of a structure that is dynamically allocated for use in a game, but there seems to be an error in the allocation, and i need a way to be able to figure out how many elements are in the array.
You could also try using the sizeof operator.

Using the example above:

number_of_elements = sizeof(map)/sizeof(map[0][0]);
// number_of_elements == 25

or

number_of_elements = sizeof(map)/sizeof(int);
// number_of_elements == 25

No sense being pessimistic. It wouldn''t work anyway.
===========================There are 10 types of people in the world. Those that understand binary and those that don't.( My views in no way reflect the views of my employer. )
Gave that a try and an getting weird values for the size of my structures, obviously im doing something wrong so ill post some code.


object -> local_vertices = new 3d_point [8];

My program has been having problems and i wanted to be able to make sure that when i initialize this structure(not usually a static value) that it was being allocated to the correct size, so i tried your suggestion.

printf("%d", sizeof(object -> local_vertices) / sizeof(3d_point));

but when i do this, it says that my local_vertices structure is 4, and the 3d_point is 16, which makes no sence to me. thanks for all your help.



Advertisement
Yes, if you use sizeof() on a dynamically allocated array, it just should return 4, because the array is just a pointer at heart, and 32 bit pointers are 4 bytes.

What kind of problems is your program having? Are you sure it''s related to allocation?

You could always go through a for loop with the array, and for each element that isn''t NULL you could print something or something like that...

But it''s possible your problem isn''t in allocation at all.

Good luck!
Without using some array classes, there is no way to test how large a dynamic array is. You need to create a variable that you set when you allocate the array, so that it will contain the size of the array you allocated.

Rock

This topic is closed to new replies.

Advertisement