Interview question and discussion in C [closed]
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 !!
My solution could be
#include <stdio.h>
#include <string.h>
/* copies "name" to `dst`.
** The caller is responsible for making sure
** `dst` has enough space */
char *fun1(char *dst) {
strcpy(dst, "name");
return dst;
}
int main(void) {
char ch[5];
printf("%s\n", fun1(ch));
return 0;
}
Here's just what I found about the snippet ...
#include<stdio.h>
I'd add a space before the header: #include <stdio.h>
; but that's just for looks
void main()
WRONG! main
returns an int
. ALWAYS!
And it should be one of: int main(void)
or int main(int argc, char **argv)
if you need parameters
{
char *ch;
ch=fun1();
No prototype for fun1
in scope. This makes the compiler assume the function returns a value of type int
and the statement tries to assign that value to an object of type char*
which is illegal: the compiler must issue a diagnostic here.
printf(ch);
}
fun1()
In C99, it is mandatory to specify a return type; in C89, int
is assumed. Also this function definition really should also be a prototype (in both C89 and C99) and specifiy the number and types of parameters (or void
if it takes none).
{
char *arr[100];
Why do you need an array of 100 pointers? ???
strcpy(arr,"name");
Oops ... no prototype for strcpy()
in scope. Also, assuming the strcpy
is the one declared in <string.h>
, the 1st parameter should be a char*
not a char **
return arr;
arr
ceases to exist right after the function returns. Its address (the array decays to the address of its first element in this context) is unusable in the calling code.
}
Either a static char arr[100]
inside thefunction (this would lead to problems, because it would render it non reentrant) or you should malloc()
the array and return its pointer (you should free it later in the caller function).
Don't wait for that company to call back ;)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void fun1();
int main()
{
char *ch = malloc(sizeof(char) *100);
fun1(ch);
printf("%s\n", ch);
free(ch);
}
fun1 (char* ch)
{
strcpy(ch,"name");
}
This is the best solution imo. There are obviously other ways to do it. You could declare char arr[100]
and pass that in, then set ch = arr, but this basically cuts out the middleman.
Although I suppose this changes the problem format, so I'm not sure if that's allowed. If you have to maintain the structure, then static char arr[100]
or malloc()
in the function is the way to go.
I see at least the following additional errors:
- The return type of
main()
should beint
, if you're following modern standards. - The
puts()
function should be used instead ofprintf()
, since no formatting is performed. - The variable
arr
should probably have typechar*
, notchar**
(a single string vs. and array of 100 strings.) fun1()
is called before it is declared & defined, which is an error. In a real project, you would have a forward-declaration in a header file which accompanies you program code.- The program, in its current form, does nothing at all.
You should start by asking your interviewer: "What is the program supposed to do?" before you try to fix it, IMO. If the point is to output "name" to std output, the following code suffices, and is much simpler (that is, better):
#include<stdio.h>
int main() {
puts("name");
return 0;
}
static declaration of arr in function
static char arr[100];
this way its still local to function, and you can return a valid pointer to it.
btw, fun1 return type should be char*
, not char**
Hence the string returned by the function fun1
is fixed,
func1
can return a const char*
type.
The string "name"
will be stored in read only area
and its pointer will be returned.
#include<stdio.h>
int main(){
const char *ch;
ch=fun1();
printf("%s", ch);
return 0;
}
const char *fun1(){
const static char* arr[] = "name";
return arr;
}
You will be able to write as below:
const char *fun1(){
return "name";
}
精彩评论