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
Post a Comment