Someone tell what is wrong with my code.

#include <iostream>

using namespace std;

int main()
{
int *p , temp ;
p = new int [5];
temp = p;
for ( int i = 0 l i < 5; i ++)
{
*p = i ;
p ++;
}
p = temp ;
for ( int i = 0; i < 5; i ++)
{
cout << p [ i ];
{
delete p [];
int q [5];
q [0]=0;
q ++;
return 0;
}
when you declare two pointers inline you have to use * for both;

int *p, *temp;

Didn't look at the rest, that just popped out.
And this is a classic reason to declare variables one per line, and wait until there is a value to initialise with:

1
2
int* p = new int [5];
int* temp = p;


Is there a reason why you wish to use new and delete ? They are not recommended. Consider using a STL container like std::vector instead. Most of the STL containers store their data on the heap.
Topic archived. No new replies allowed.