tags:

views:

142

answers:

6

I am trying to print print out an array of integers whose length i dont know in C++. Here is my attempt.

    int i = 0;
    while ( X != NULL){
            cout << *(X+i) << " ";
            i+=1;
    }

X is the array. My problem is to stop upon printing last element.

+4  A: 
int i = 0; 
while ( *(X+i) != NULL){ 
        cout << *(X+i) << " "; 
        i+=1; 
} 

Assuming your array is null-terminated.
However it would be much much better if you kept track of the length of your array, or even better use a vector.

James
It's an array of integers, so it's a fair bet that it's _not_ null-terminated. Nevertheless, there might be a magic number he could use to terminate the array.
Peter Ruderman
Using `NULL` when you mean zero doesn't help readability.
Mike Seymour
@Peter Ruderman, that may also be the problem. It seems to be a classic moving from c to c++ mistake.
James
Using special characters is dangerous in terms of maintenance (you know it now but there is implied knowledge that must be handed down to the next generation of maintainers (some of whom own axes).
Martin York
+1 for vector! From the OP's perspective, it's like an array with a `size()` method.
Daniel Earwicker
+2  A: 

You could find out the size of the array if X is an array and not a pointer:

int X[unknownsize];

size_t arraysize = sizeof(X)/sizeof(int);

for(int i=0;i<arraysize;i++)
{
  cout << X[i] << " ";
}
This can be dangerous. If the code is re factored in-to a function (a likely scenario for such a small useful reusable piece of code) it will likely stop working.
Martin York
True, but it answers the question Vaolter asked.
void printX(int array[]){ int size_array = sizeof(array)/sizeof(int); for (int i = 0; i < size_array; i++) cout << array[i] << endl;}I have put it as a function but i try it it give zero.
Vaolter
as Martin said, once you make it a standalone function, you are working with a pointer, not an array. My answer applies to arrays as your question stated. If all you have is a pointer, you will have to try some of the other suggestions.
+8  A: 

Either the last element in your array will have to be some magic value (0 or -1 or INT_MAX or something similar) that you can watch for and thus use it to stop looping. Or else you must find a way to record the length of the array.

There is no standard way in C++ to determine the length of an arbitrary array.

The other alternative is to stop using raw arrays and use a smarter object such as std::vector which gives you array-like access but also keeps track of the number of elements stored in it.

TheUndeadFish
Or `std::/boost::array`.
GMan
A: 

The loop you've written never modifies X, so assuming it's a valid array the loop will loop forever (or until it crashes by running some amount off the end).

The C++ way to solve this is to not use arrays but to use std::vector instead. It keeps track of all the bookkeeping including length for you automatically.

If X is an actual array (like int X[5];) you can use (sizeof(X) / sizeof(X[0])) to get the number of elements in the array. If X is passed as a pointer then there is no way in C or C++ to get the length. You must pass the length along with the array in that case.

You could play games with "magic" numbers in the array that mean end (for example if it must always be positive you could use -1 to signal the end, but that's just asking for obscure bugs. Use std::vector.

Mark B
+1  A: 

First you need to find the size of the array and iterate until the size of the array to output the array elements as below,

size_t size = sizeof(X)/sizeof(X[0]);

for (size_t i=0; i<size; i++)
{
    cout << X[i] << " ";
}
Prabhu Jayaraman
Why bother with the cast? Fix the types used instead, i.e. make your loop index variable a `size_t` to match the type of your `size` value.
Void
Thanks Void. updated.
Prabhu Jayaraman
A: 

If you declared the array there's an old C trick to get it's size:

int  array[10];

for ( int i = 0; i < sizeof(array) / sizeof(array[0]); i++ )
  printf( "%d", array[i] );
Jay