#### Archived

This topic is now archived and is closed to further replies.

# Arrays

## Recommended Posts

how can i find out the "length" of an array? I remember JScript would just do array.length but i dont think that works in c++ and im new to it.

##### Share on other sites
I am pretty sure you can''t in C++. You can use the Vector class. This works just like an array and can also tell you the size.

First make it work,
then make it fast.

--Brian Kernighan

"The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were." - John Fitzgerald Kennedy(35th US President)

##### Share on other sites
You can try;

int items = sizeof(array) / sizeof(array[0]);

That sometimes works for me, but I have failed to write a function to do it, passing arrays isn''t that simple. Using an std::vector would probably be more suitable.

##### Share on other sites
?

just store the length somewhere... and you know it because you was the one who decided it....

##### Share on other sites
Or you could give all the items in the array a starting value like for integers -1 and then check the value. Once you find -1 you know you reached the end of the array...

##### Share on other sites
Well if your array is statically sized such as
array[20]; then you can use the size of operator to determine it''s size. If it''s a dynamically sized array you need to keep track of that yourself, or use std::vector.

##### Share on other sites
C and C++ don't keep track of length at all. You must do this yourself.

If the number of elements in an array is constant, you can just code around that number:

#define NUM_INTS 256
int array_of_death[NUM_INTS];
:
:

for (int i = 0; i < NUM_INTS; i++)
{
printf("%d is the NUMBER OF DEATH!\n", array_of_death);
}
:
:

For CZ-strings (char* strings), you do not keep track of the number directly. Instead all CZ-strings end with a '\0' or NULL byte. All STD string functions expect the last byte of a string to be a 0 or you will get LOTS OF segmentation faults.

char buffer[] = "My really cool buffer"; // initialized string....
char name[256]; // uninit'ed string

strcpy(name, buffer); // copies buffer to name

printf("Name is %d bytes long\n", len(name) ); // len counts the number of bytes from the start of the string to the NULL terminator

You can create a class or struct to keep track of the length yourself. That's how Vectors work.

class Vector
{
private:
int* array; // this pointer will hold the array
int length; // this will hold the length of the array
public:
Vector();
Vector(int init_count);
~Vector();
void length(); // this simply returns length
int get(int index); // this checks to make sure index is greater than 0 and less than length, and if it is, returns array[index]
}

Java / javascript sacrafice a little bit of speed for a lot of safety:

// javascript
var array[5];
for (var i = 0; i <= 5; i++)
{
array[i] = 0; // will flag an error when i = 5...
}

// C++
int array[5];
for (int i = 0; i <= 5; i++)
{
array[i] = 0; // will silently write past the array.... a potentially serious and hard to find error
}

C++ allows more freedom than javascript. Whether you use that freedom to make code that runs 10x faster than javascript or use it to "shoot yourself in the foot".... that's up to you.

/¯\_/¯\_/¯\_/¯\_/¯\_/¯"You TK'ed my chicken!"
\_/¯\_/¯\_/¯\_/¯\_/¯\_/

[edited by - Tac-Tics on September 2, 2003 2:42:09 PM]

##### Share on other sites
Thanks for all the info. I''m gonna use a vector.

##### Share on other sites
quote:
Original post by hammerstein_02
int items = sizeof(array) / sizeof(array[0]);

That''s what I use.

##### Share on other sites
C standard strlen() would not work?

##### Share on other sites
strlen() is for the number of characters in a null-terminated C-style string.

You could use a template nontype parameter.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

##### Share on other sites
like whats his face said before a loop can do it to

main()
{
int the_array[100]; //whatever number even user made

int num = 0;
for(int blah = 0; blah != NULL; blah++)
num++;

}

this will loop till the end of the array because the end is the null terminator

##### Share on other sites
quote:
Original post by Anonymous Poster
like whats his face said before a loop can do it to

main()
{
int the_array[100]; //whatever number even user made

int num = 0;
for(int blah = 0; blah != NULL; blah++)
num++;

}

this will loop till the end of the array because the end is the null terminator

no it won''t

##### Share on other sites
template <class T, std::size_t N>
std::size_t array_size(T(&)[N]) { return N; }

Seriously, though, use vector.

• ### Forum Statistics

• Total Topics
628390
• Total Posts
2982412

• 10
• 9
• 19
• 24
• 11