This was the question asked to me in an interview in c:
#include<stdio.h>
void main(){
char *ch;
ch=fun1();
printf(ch);
}
fun1(){
char *arr[100];
strcpy(arr,"name");
return arr;
}
I was given the above program and was asked to figure out the problems in the above code. below was my answer
- function declaration is wrong.the
return type should be
char **
- syntax of
printf
is wrong arr
scope is limited to the functionfun1
then
Interviewer : what would be your solution to the problem?
Me: you need to make the arr variable as global and fix the remaining issues mentioned above.
Interviewer: Dont you think global variables are dangerous?
Me: Yes ofcourse,since we cannot say where it is being accessed in which functions and sometimes it gets almost impossible to find which function has changed the value
Ineterviewer :give me a solution without a global variable
Me:????
what would be your solution for this? Could anybody pls point out the errors that i have made !!