I have written following C code:
#include <stdio.h>
#include <stdlib.h>
int *getPointer(int var);
void anotherFunction();
int main ( int argc , char * argv [])
{
int *intPtr = getPointer(3);
printf("%d\n",*intPtr);
anotherFunction();
printf ("%d\n",*intPtr);
getPointer(5);
printf("%d\n", *intPtr);
return EXIT_SUCCESS ;
}
// Which problem occurs here?
int *getPointer(int var) {
int *ptr=&var;
return ptr;
}
void anotherFunction(){
// do nothing
int a [ 5 ] = { 4 , 5 , 6 , 7 , 8 };
}
The Output is:
3
7
5
I do not understand why the value of intPtr changes in the second printf() call. I would appreciate your help! Thank you