Simple Pointer

Hello, I have this simple code which compiled and contain no error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void add(int x, int y, int* result)
{
   *result = x + y;
}

int main()
{
   int* result;
   int* newptr;

   add(2,3, result);
   cout << result << endl;
    return 0;
}

However the program crashes when I ran it, but when I took out
int* newptr; the program ran fine. Could someone tell me why it crashes? I have a feeling that it could be because result and newptr are both pointing at the same address, but I don't think it is correct.
Last edited on
The error is from no memory being allocated to result.
Allocate with new http://ideone.com/lOWVRE
Or better pass by reference http://ideone.com/R2vzOE
Last edited on
Thank you for the explanation naraku9333.
Topic archived. No new replies allowed.