Can you return an integer by dereferencing a pointer?
c++, pointers, undefined-behavior
Solution
Why does this code give me a run time error?
Because `y` is a `NULL` pointer, which is dereferenced in `f()`. Note, it is undefined behaviour to dereference a `NULL` pointer.
Can you return an integer by dereferencing a pointer?
Yes, assuming the pointer is pointing to a valid `int`. For example:
int main()
{
int y = 0;
int z = f(&y);
}
Problem
``` int f(int *x) { *x = 5; return *x; } int main() { int * y = 0; int z = f(y); } ``` Why does this code give me a run time error?