why do I get an exception with Null pointer?

Hi guys I'm just curious as to why if I pass a the variable z by ref which points to NULL to my function I get an exception,

why does this occur?

Thanks

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
26
   
  #include <iostream>

using namespace std;

void val(int x);
void reff(int *x);

int main()
{

   int *z = NULL;
   reff(z);

}

void val(int x){

    x = 23;
}

void reff(int *x){

   *x = 30;

}
Simply, there is no value pointed to by a null pointer, so trying to access the value makes no sense. It is like trying to determine what integer is the result of dividing by 0; there is no such integer so cannot get a result.

The program cannot continue normally at this point because it would require this non-value to be stored as a value, and so you exit the control flow via exception.
thanks
Topic archived. No new replies allowed.