to print out an array

Im trying to print out a simple array such as P[]={1,2,3,4,4,1,2}
why do i get this weird output -> 0x28fec8 instead?
can anyone advise what is wrong with my code that i cant even print out a simple array?

1
2
3
4
5
6
7
8
#include <iostream>

using namespace std;

int main(){
double P[]={1,2,3,4,4,1,2};
cout<<P;}
What you're printing is the memory address of the array and not the values within it. An array is basically a pointer to an int. So when you cout a pointer without de-refrencing it, it will display the address in memory that the pointer is stored.

To display a value in an array, you'll need to cout P[0] or P[1] and ect for each value in the array. If you want to print them all, you will need to use a for loop and put the counter in the index.

Here's a little reference to arrays and gives an example of how to print one using a function:

http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
Topic archived. No new replies allowed.