A
ak
Recently at an interview i was asked the following question :
Assuming the function lookupName is defined, what's wrong with this
code (hint: 2 bugs)?
const char *getName(const char *c) {
std::string name = lookupName(c);
if (name == NULL)
return "Anonymous";
return name.c_str();
}
int main(int argc, char *argv[]) {
const char *name = NULL, *c = NULL;
if (argc >= 2)
c = argv[1];
name = getName(c);
printf("My name is %s\n", name);
return 0;
}
MY SOLUTION : #bug 1: name==NULL is illegal
#bug 2: name=getName(c) should be included in
the 'if' statement so that NULL will
not be
passed to getName()
The above was my solution...what do you all experts think?
Am i right or have i missed something?
Cheers,
ak
Assuming the function lookupName is defined, what's wrong with this
code (hint: 2 bugs)?
const char *getName(const char *c) {
std::string name = lookupName(c);
if (name == NULL)
return "Anonymous";
return name.c_str();
}
int main(int argc, char *argv[]) {
const char *name = NULL, *c = NULL;
if (argc >= 2)
c = argv[1];
name = getName(c);
printf("My name is %s\n", name);
return 0;
}
MY SOLUTION : #bug 1: name==NULL is illegal
#bug 2: name=getName(c) should be included in
the 'if' statement so that NULL will
not be
passed to getName()
The above was my solution...what do you all experts think?
Am i right or have i missed something?
Cheers,
ak