On 2/15/07, Thomas Hruska <[EMAIL PROTECTED]> wrote:
> Brett W. McCoy wrote:
> > On 2/15/07, shahwalishaik <[EMAIL PROTECTED]> wrote:
> >
> >> I've attended an interview few days back. The qeustion paper had
> >> some programs. There's a bug in it, and we need to find it. Please,
> >> help me find the bug.
> >>
> >> #include<string.h>
> >> #define MAX_STRING 80
> >> char *Funct(char *p1, char *p2)
> >> {
> >> char str[MAX_STRING+1];
> >> int 1;
> >>
> >> str[MAX_STRING] = '\0';
> >> 1=strlen(str);
> >> strncat(str,p2,MAX_STRING-1);
> >> return str;
> >> }
> >>
> >>
> >> [mod - There's more than one bug. Simply putting this code through a
> >> compiler would show you some syntax errors - you cannot use the numeral
> >> one as a variable name (the
> >> int 1; and
> >> 1=strlen(str);
> >> lines. A runtime error is the potential buffer overrun in the call to
> >> strncat() - PJH]
> >
> > The function also returns the address of the local str variable.
>
> The function also has the bug of not doing anything useful even if it
> did work. Best bet is that it was supposed to concatenate two strings.
>
> I'd prefer the Safe C++ method of doing string concatenation:
But if they were using C++, they could have used std::string instead
of C's string manipulation functions....
> printf("%s\n", *str);
Is printf() part of Safe C++? ;)
--
PJH
Aio, quantitas magna frumentorum est