The best way is to keep track of how many integers you've stored with a counter-variable.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
That's the only way to do it when using basic C-style arrays. If you are writing a c++ program then use std::string instead of char* and you will be able to easily get the array size whenever you need it.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
No. Checking the length of a char-array means looking for the first '\0' (end of string). But there is no 'end of int'-int...
But you're using C++, so why not use an int-stack instead of an array?
example:
stack< int > myStack; //init the stack
for (int i = 0; i < 5; i++)
{
myStack.push(i); //push on stack (add to array)
}
while (!myStack.empty()) //as long as there are ints on the stack
{
cout << myStack.top() << endl; //show the number that's on top of the stack (last of the array)
myStack.pop(); //remove the number from stack
}
I didn't test it, but you probably get the idea. This way you don't have to worry about how many ints are in an array.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
That's the only way to do it when using basic C-style arrays. If you are writing a c++ program then use std::string instead of char* and you will be able to easily get the array size whenever you need it.
Agreed, but the OP asked for an array of ints, not a char-array.
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403