c++ - Can you return an integer by dereferencing a pointer? -


int f(int *x) {     *x = 5;     return *x; }  int main() {     int * y = 0;     int z = f(y); } 

why code give me run time error?

why code give me run time error?

because y null pointer, dereferenced in f(). note, undefined behaviour dereference null pointer.

can return integer dereferencing pointer?

yes, assuming pointer pointing valid int. example:

int main() {     int y = 0;     int z = f(&y); } 

Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -