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;
}
You can’t just
coutan array. Arrays are notcout-able. Whenever you successfullycoutsomething of typeT, it means that there’s a dedicated overloaded<<operator designed specifically tocoutvalues of typeT. And there’s no dedicated<<operator forcout-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
coutthe values of all elements of your array, you’ll have to eithercoutthem manually, one by one, or use some standard algorithm that can do it for you. For example