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

sql - VB.NET Operand type clash: date is incompatible with int error -

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

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -