Filling in a Dynamic Array dynamically

What's wrong with this code? It only executes the for loop once for index=0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main()
{
 int *numberArray = new int[5];
 int *counter = new int(0);
 
 for(;*counter<5;*counter++)
 {
  cout<<"ho ho"<<endl;
  numberArray[*counter]=*counter;                         
 }   
 
 for(*counter=0;*counter<5;*counter++)
 {
  cout<<"haha"<<endl;
  cout<<numberArray[*counter]<<endl;
 }
 
 delete numberArray;
 delete counter;
 system("pause");
}

(*counter)++

why are you dynamically allocating an int anyway???
Many things are wrong with this code.
Line 6: You're allocating a raw array instead of using std::vector.
Line 7: You're creating an int with new instead of using an automatic object.
Line 9/15: You're not declaring the counter variable inside the for loop.
Line 9/15: You're using the name "counter" instead of "i", which is the established standard name for a generic counter variable.
Line 9/15: You're incrementing counter instead of the value it points to. You're missing a pair of braces here.
Line 9/12/15/18: Ugly and unnecessary dereferencing, which gets back to line 7.
Line 21: You're using delete on an array allocated with new[]. This requires deletion with delete[].
Line 21/22: Manual deletes, which gets back to line 6+7.
Line 23: http://www.cplusplus.com/forum/beginner/1988/ and http://www.cplusplus.com/forum/articles/11153/
Topic archived. No new replies allowed.