I get a runtime error while trying to run the code below.
- The function get() returns a void pointer where the user input is stored.
- The function getShort() calls the get() function and typecasts and dereferences the pointer to short before returning its value.
- While the value works perfectly fine inside getShort(); any other method that call it will get the following runtime error.
The instruction at Ox000000000040002C referenced memory at Ox000000000000002C. The memory could not be written.
void * get(char formatSpecifier[]){
void *ptr;
scanf(formatSpecifier, ptr);
return ptr;
}
int getInt(){
int i = *(int *)get("%d");
printf("Works perfectly fine here: %d", i);
return i;
}
int main(){
int j = getInt(); // Error thrown here.
prinf("The value is : %d", j); // Does not print;
return 0;
}
Any help or feedback is appreciated. Many thanks.