我正在尝试使用指针重写字符串附加函数。我的字符串附加函数没有显式使用指针,看起来像这样:
void append_a_string(char a[], b[]) {
int i, j;
j = length of string a;
while (b[i]) {
a[i] = b[i]
i++;
j++;
}
b[j] = 0;
}
我使用预制函数获取字符串“a”的长度。我很困惑在这种情况下如何使用指针。这是我到目前为止所拥有的:
void append_a_string(char *a, char *b) {
a = length of string a;
while (b) {
b = a;
a++;
b++;
}
b = 0;
}
最佳答案
这是精炼的代码:
void append_a_string(char *a, char *b) {
a += strlen(a);
while (*b) {
*a++ = *b++;
}
*a = '\0';
}
需要空终端的是 a
,而不是 b
。
关于c - 使用指针将字符串从源附加到目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36274668/