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

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -