Suppose I have this block of code:
#include <stdio.h>
#include <unistd.h>
int *local_pointer(void)
{
int x = 6;
return &x;
}
void add(void)
{
int a;
a = 4;
a = a + 1;
}
int main()
{
int *result;
result = local_pointer();
printf("int is %d\n", *result);
add();
printf("int is %d\n", *result);
return 0;
}
The output of this code is
int is 6
int is 5
I am confused as to why this is?
If we go through main, the result variable is returned a pointer to x and then when it the pointer is dereferenced the its value is 6 however why does calling the add function make the next printf statement print 5?