[英]Writing my own strcopy Function in C using pointers
I am trying to create my own string copy function in C.我正在尝试在 C 中创建自己的字符串副本 function。 Copying the text works, however extra characters are added to the destination string at the end and I don't know why.复制文本有效,但是最后在目标字符串中添加了额外的字符,我不知道为什么。 I would be very happy to get an answer.我会很高兴得到答复。
Here is my code:这是我的代码:
#include <stdio.h>
#include <string.h>
void copy(char *dst, char *src) {
int src_len = strlen(src);
char *src_strtng = &src[0];
char *dst_strtng = &dst[0];
for (int i = 0; i < src_len; i++) {
*dst_strtng++ = *src_strtng++;
}
printf("%s\n", dst);
}
int main() {
char srcString[] = "We promptly judged antique ivory buckles for the next prize!";
char dstString[strlen(srcString)];
copy(dstString, srcString);
}
create my own str copy function in C.在 C 中创建我自己的 str 副本 function。
Missing null character termination缺少null 字符终止
This is OP's key issue, lack of appending a null character to dst
.这是 OP 的关键问题,缺少将null 字符附加到dst
。
Only need to traverse src
once只需要遍历src
一次
Rather than strlen()
in copy()
, just look for '\0'
.而不是strlen()
在copy()
中,只需查找'\0'
。 *1 *1
Does not return a char *
不返回char *
Save the destination pointer.保存目标指针。
Missing const
for data referenced by src
src
引用的数据缺少const
Pedantic concern: str
should act as if char
was unsigned char
*2迂腐的担忧: str
应该表现得好像char
是unsigned char
*2
char *copy(char* dst, const char* src) {
unsigned char *udst = (unsigned char *) dst;
const unsigned char *usrc = (const unsigned char *) src;
do {
*udst++ = *usrc;
} while (*usrc++);
return dst;
}
Allocate enough space for the length of a string and a null character为字符串长度和null 字符分配足够的空间
strlen()
returns the length of a string . strlen()
返回字符串的长度。 That does not include the final null character .这不包括最终的null 字符。
int main() {
char srcString[] = "We promptly judged ...";
char dstString[strlen(srcString) + 1]; // Add 1
copy(dstString,srcString);
}
*1 C's strcpy()
is char *strcpy(char * restrict s1, const char * restrict s2);
*1 C 的strcpy()
是char *strcpy(char * restrict s1, const char * restrict s2);
Note the restrict
.注意restrict
。 This implies access via the pointer is not affected by other code.这意味着通过指针进行的访问不受其他代码的影响。 Eg source string and destination will not overlap.例如源字符串和目标不会重叠。
// Better as
char *copy(char* restrict dst, const char* restrict src) {
OP's use of strlen()
in copy()
is a good first step to handle overlapping memory (see following), but strcpy()
does not need to handle that - suggest copy()
not deal with overlap. OP 在copy()
中使用strlen()
)是处理重叠 memory 的良好第一步(见下文),但strcpy()
不需要处理 - 建议copy()
不处理重叠。
char *copy_handle_overlap(char* dst, const char* src) {
return memmove(dst, src, strlen(src) + 1);
}
*2 C string functions have: *2 C 字符串函数有:
For all functions in this subclause, each character shall be interpreted as if it had the type
unsigned char
(and therefore every possible object representation is valid and has a different value).对于本子条款中的所有函数,每个字符都应被解释为好像它具有unsigned char
类型(因此每个可能的 object 表示都是有效的并且具有不同的值)。
This is important for the rare non-2's complement to distinguish +0
from -0
.这对于区分+0
和-0
的稀有非 2 补码很重要。
Non-2's complement support expected to be dropped with C2X.预计 C2X 将放弃对非 2 的补码支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.