初歩的すぎてすみませんが、以下のようにstrcpyのライブラリ関数を実装してみました。strncat()
#include <stdio.h>
void strncat (char *s, char *t, int n) {
// malloc to extend size of s
s = (char*)malloc (strlen(t) + 1);
// add t to the end of s for at most n characters
while (*s != '\0') // move pointer
s++;
int count = 0;
while (++count <= n)
*s++ = *t++;
*(++s) = '\0';
}
int main () {
char *t = " Bluish";
char *s = "Red and";
// before concat
printf ("Before concat: %s\n", s);
strncat(s, t, 4);
// after concat
printf ("After concat: %s\n", s);
return 0;
}
コンパイルして正常に実行されます...ただし、まったく連結されません!
フィードバックをお寄せいただきありがとうございます...ありがとう!