dynamic array

Dec 15, 2012 at 8:57am
Below is a program which i wrote i want to make a dynamic array,my program is getting input.it should output the integers which i have entered but the program is displaying the addresses of the the input integers....
#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
int *p=NULL;
int n;
cout<<"Enter the size of the array";
cin>>n;
p=new int[n];
cout<<"Enter the elements of the array";
for(int i=0;i<n;i++)
cin>>*p++;
cout<<"Entered elements in the array are:";
for(int i=0;i<n;i++)
cout<<p++<<endl;
system("pause");
}
Dec 15, 2012 at 9:18am
That's because you're printing the addresses:
cout<<p++<<endl;
Last edited on Dec 15, 2012 at 9:20am
Dec 15, 2012 at 9:19am
Change your last cout statement to *p++. Without the *, you're using the memory location of p, or rather, what p points to. You did it right in the step above.
Dec 15, 2012 at 11:18am
closed account (D80DSL3A)
Ignored advice removed.
Last edited on Dec 16, 2012 at 9:36am
Dec 15, 2012 at 3:24pm
p++ means that you are printing the address of the values change that and it will work.
Dec 16, 2012 at 8:39am
can any one paste correct version of my above code...?
Topic archived. No new replies allowed.