Below is the code
The Code:
#include <stdio.h>
int * num(void);
int main(void)
{
int * num2;
num2 =num();
printf("%d\n" , *num2);
return 0;
}
int * num(void)
{
int num = 20;
return #
}
The Question :
As we know , the function
numis local to its functionnum(), so in this code I try to return the address of the variablenumin the function to the function that calls it , which ismain().After that I just use the dereferencing operator to extract the value of the specific
numvariable and print it out inmain()function.There's one thing i'm confused . I remember i read a book about javascript that mention a variable lifetime is within the function , which mean after the function finish performing its instructions and pass the control back to the function that calls it , the value of each variable in the function will be clean out(garbage collector).But why in this code my
main()function still can point to the value of that specific memory address??