pointers

#include

void fun(int *p)

{

int q = 10; p = &q;

}

int main()

{

int r = 20;

int *p = &r;

fun(p);

printf("%d", *p);

return 0;

}

But in fun it is pointing to address of q.In main function we are trying to print the value by *p thats means we are trying to access q which has been deleted

1Comment
Gaurav @gauravchaudhari
1 Dec 2017 04:28 pm
I guess you are mistaking it as pass by reference.
It's not pass by reference. The changes made in function fun are done in local pointer variable *p of that function.