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' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -