gpt4 book ai didi

C:strcat和strcpy函数如何实现?

转载 作者:行者123 更新时间:2023-11-30 19:35:20 24 4
gpt4 key购买 nike

我正在尝试为学校作业实现这些功能。我知道我的程序的逻辑是什么,但它的行为有点奇怪。我希望得到一些帮助

这是我的代码:

#include <stdio.h>
#include <stdlib.h>


char *StrCat (char *destination, const char* source) {
destination = malloc(sizeof(source) + sizeof(destination));
char *temp = destination;
while (*temp != '\0') {
printf("temp: %c - ", *temp);
temp++;
}
int i;
for (i = 0; source[i] != 0; i++) {
*temp = source[i];
}
temp[i++] = 0;
return destination;
}

char *StrCpy(char *destination, const char* source) {
destination = malloc(sizeof(source) * sizeof(char));
int i;
for (i = 0; source[i] != '\0'; i++) {
destination[i] = source[i];
}
destination[i++] = '\0';
return destination;
}

int main ()
{
char *src;
char *dest;

src = StrCpy(src, "A source sentence");
dest = StrCpy(dest, "This is the destination sentence");

printf("%s\n%s\n", src, dest);

StrCat(dest, src);

printf("Final destination string : |%s|", dest);

return(0);
}

这是输出

A source sen
This is the destination sentence
temp: i - temp: n - temp: a - temp: t - temp: i - temp: o - temp: n - temp: - temp: s - temp: e - temp: n - temp: t - temp: Q - temp: e - temp: s - temp: t - Final destination string : |This is the |

另外,当我注释掉

dest = StrCpy(dest, "This is the destination sentence");

行,我得到这个输出

A source sentence
(null)
temp: e - Final destination string : |(null)|

我的问题是:为什么当我不在 dest 上调用 StrCpy 时 src 打印正确?另外,在第一个输出中,为什么 temp 从字母 i 开始,而它应该指向目标的第一个元素?

谢谢

最佳答案

您需要小心使用 sizeof()

在以下情况下,sizeof() 将返回系统上指针的大小(可能是 4/8 字节),而不是 >char,或字符串的长度(这就是您使用它的方式)。

char *myString;
printf("sizeof(myString): %d\n", sizeof(myString));

您实际上要寻找的是指针所指向的类型的大小...为此,您需要“取消引用”指针一次。这将为您提供单个 char 的大小。

char *myString;
printf("sizeof(myString): %d\n", sizeof(*myString));

要获取字符串的长度,需要使用strlen():

char myString[] = "Testing 123...";
printf("string length: %d\n", strlen(myString));
<小时/>

接下来,您需要检查您对 strcpy() 的理解...标准strcpy(3)将期望sourcedestination都是现有缓冲区。它还期望目标有足够的存储空间来容纳整个源,加上一个 nul 终止符 ('\0')。如果情况并非如此,那么您的应用程序可能(希望会)因段错误而崩溃,或覆盖其他变量。这是工业界经常不赞成使用 strcpy() 的原因之一 - 更喜欢使用 strncpy()

The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.

<小时/>

我更希望看到您使用以下原型(prototype)实现函数,如果您的导师提示,那么您的作业的扩展是理解为什么具有 n 参数很重要,并向他们解释.

/* append the 'source' to the 'destination', allowing the total length of 'destination' to
* be no longer than 'n' bytes, including the terminating nul */
char *StrnCat (char *destination, const char* source, size_t n);

/* copy the 'source' to the 'destination', allowing the total length of 'destination' to
* be no longer than 'n' bytes, including the terminating nul */
char *StrnCpy(char *destination, const char* source, size_t n);

如果您在 main() 函数中为字符串分配存储空间,那么您根本不需要担心 malloc()

这是一个学习机会,所以我不会为您实现它们。

关于C:strcat和strcpy函数如何实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com