2015年3月30日星期一

Find the bug in this code(1)


The wrong code looks like this:












1. In this code, malloc function has no specific value, but from the function prototype, we know the default return value is (void *). So we have to use typecast(char*). 
2. Another problem in this code is  p += strlen(name); Now p has point to the end of the first string, so after assign p with '@', the *p is '@', with 'gre' disappears. So the result will be '@research.att.com'. 
3. in the strlen usage, now we prefer strcpy_s(which is in the package window.h). MS went on a "these standard functions aren't safe!"rampage a couple years ago and deprecated several standard functions, replacing them with _s(safe) alternatives.
The right code is:







The result shows on the console.

没有评论:

发表评论