ポインターを使用して文字列の一部を別の文字列にコピーしようとしています。カウントを超えても停止しませんが、結果の文字列は正しい場所でコピーを開始します。また、文字列は、結果パラメーターからではなく、ソース文字列からコピーされません
#include <stdio.h>
char *getSub(const char *orig, int start, int count, char *res);
int main(void)
{
const char orig[] = "one two three";
char res[] = "123456789012345678";
printf("%s\n",getSub(orig, 4, 3, res));
return 0;
}
char *getSub(const char *orig, int start, int count, char *res)
{
const char *sCopy = orig;
while (*orig)
{
if (start >= (orig - sCopy)) && (res-sCopy < count))
{
*res++ = *orig++;
}
else
*orig++;
}
return res;
}