user1241357
user1241357

Reputation:

My code prints out the address instead of the array value?

What is wrong with my code? I want to print out the array, but when I try to do so it seems to print out an address instead.

#include <iostream>
#include <ctime>
#include <stdlib.h>

using namespace std;

int main ()
{
    srand(time(NULL));

    int array[9]= {0};

    for (int i=0;i<=8;i++)
    {
        array[i]= (rand()%101);
    }

    cout<< array;

    system ("PAUSE");
    return 0;
}

Upvotes: 0

Views: 2252

Answers (5)

AnT stands with Russia
AnT stands with Russia

Reputation: 320541

You can't just cout an array. Arrays are not cout-able. Whenever you successfully cout something of type T, it means that there's a dedicated overloaded << operator designed specifically to cout values of type T. And there's no dedicated << operator for cout-ing arrays (aside from strings). For this reason the compiler chooses the closest match for the given argument type: a << operator for pointers. Since arrays are convertible to pointers, that << is applicable here.

If you want to cout the values of all elements of your array, you'll have to either cout them manually, one by one, or use some standard algorithm that can do it for you. For example

std::copy(array, array + 9, std::ostream_iterator<int>(std::cout, " "));

Upvotes: 4

Begemoth
Begemoth

Reputation: 1389

C++ decays array type to a pointer type when the value is passed as argument to a function. C++11 has std::array (and TR1 specifies std::tr1::array) which retains the size of the array. Here is your example modified to use std::array:

#include <array>
#include <iostream>
#include <ctime>
#include <stdlib.h>

template<typename T, std::size_t N>
std::ostream& operator<< (std::ostream& ostm, const std::array<T, N>& a)
{
  for (auto const& x : a)
    ostm << x << ' ';

  return ostm;
}

int main ()

{
    srand(time(NULL));

    std::array<int, 9> array;
    for (auto& x : array)
        x = rand() % 101;

    std::cout<< array << std::endl;
}

Upvotes: 0

johnsyweb
johnsyweb

Reputation: 141810

std::cout doesn't have an overload for int array[9], so this decays to a pointer (int*) and this is what you'll see displayed (something like 0x7fffe47142d0).

To print the ints in the array individually, you will need to use a loop construct (like the for- loop you are using for populating the array) and send each of the ints to std::cout in turn, perhaps with some whitespace to format them.

Once you get the hang of C++ and its standard library, you may want to investigate how to do this with std::copy(), but I suspect this is a bit advanced for your homework.

Upvotes: 0

Mahesh
Mahesh

Reputation: 34625

You need to iterate over the array and print each element like how you assigned value at each index. Since array decays to a pointer to the first element in the sequence, you are getting the address.

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308206

You'll need to do a loop to output each array element on its own.

The problem is that C++ doesn't always know the size of the array, so it can't default to outputting the whole thing as you would expect.

Upvotes: 2

Related Questions