本文介绍了strcat的实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试自己实现的strcat,我发现从维基中的strcat的实施像这个......但是当我使用它,有段错误。
这有什么错低于code?
的char *strcat的(字符* DEST,为const char * SRC){ 为size_t I,J; 对于(i = 0; DEST [I] ='\\ 0';!我++) ; 为(J = 0; SRC [J] ='\\ 0';!J ++) DEST [I + J] = SRC [J]。 DEST [I + J] ='\\ 0'; 返回DEST;}解决方案
在code是好的。
看起来你必须在调用code的一个问题。
你记得为目标字符串分配足够的内存?
I tried to implement the strcat by myself, and I found the strcat implementation from Wiki like this......but when I use it, there is segmentation fault.
What's wrong with the code below?
char * strcat(char *dest, const char *src) { size_t i,j; for (i = 0; dest[i] != '\0'; i++) ; for (j = 0; src[j] != '\0'; j++) dest[i+j] = src[j]; dest[i+j] = '\0'; return dest; }解决方案
the code is okay.
Looks like you have a problem in the calling code.
Did you remember to allocate enough memory for the target string?
更多推荐
strcat的实施
发布评论