- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为学校作业实现这些功能。我知道我的程序的逻辑是什么,但它的行为有点奇怪。我希望得到一些帮助
这是我的代码:
#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)
将期望source
和destination
都是现有缓冲区。它还期望目标有足够的存储空间来容纳整个源,加上一个 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/
这个问题在这里已经有了答案: implicit declaration of function itoa is invalid in c99 (3 个答案) C strcat - warning:
这个问题在这里已经有了答案: Why do I first have to strcpy() before strcat()? (5 个答案) 关闭 3 年前。 如题,我做了一个demo来说明一下我
我在使用下面的程序时遇到问题。我正在尝试扫描用户输入的特定单词的字符串命令。我现在的主要问题是,当我运行以下命令时,我收到一条警告,指出“传递 ‘strcat’ 的 arg 2 使指针来自整数而不进行
我知道发生这个 valgrind 错误是因为我试图使用未初始化的东西。下面的代码是导致此错误的代码。它正在做的是尝试读取 Racket 代码并获取每个符号,例如 + 或 define。 (标记化)我不
原型 extern char *strcat(char *dest,char *src); 用法 &n
让我们举个例子。 #include #include int main() { char str1[7] = "hello "; printf("Initial size of s
我试图在不改变单词顺序的情况下颠倒一个句子, 例如:"Hello World"=> "olleH dlroW" 这是我的代码: #include #include char * reverseWo
根据Allocate string (char*) in another function我现在有这样的代码: void someFunction(char** string, char** arg
我想不通。当我在我的 Windows 机器上使用 Code::Blocks 编译这段代码时,它工作得很好,但是当我尝试在 Cygwin 或学校的实际 Unix 机器下使用 Make 编译它时,我得到下
我在一些遗留代码中发现了这一点。 static char title1[] = "SUMMARY REPORT"; static char title2[] = "PERIOD: "; ... str
我正在尝试使用 C 中的指针和 strcat。这是我学习过程的一部分。 想法是用户输入一个包含数字的字符串,输出应该只返回数字。所以,如果用户输入te12abc 输出应该是 12。 这是我的第一次尝试
在对字符串进行一些程序时,我遇到了这个小问题。他们向我提出的问题是——编写函数 strcat(s,t) 的指针版本,它将字符串 t 复制到 s 的末尾。我把程序写成这样 - #include void
我在 strcat 和段错误方面遇到了一些问题。错误如下: Program received signal EXC_BAD_ACCESS, Could not access memory. Reaso
当运行以下故意堆栈粉碎代码时,strcat 将 source 的值复制十次。 #include #include int main() { char a[16]; char b[1
我在 C 中有一个函数,我试图从两个不同的位置(未知大小,可能很大)获取字符串并将它们组合成一个字符串并返回它们。如果我只打印两个字符串,那么我会得到正确的结果,但是当我尝试使用 strcat 组合字
void mystrcat(char* to, const char* from) { while (*to) to++; while (*from) *to++ = *from++;
这是 295c 的问题之一 #include #include main() { char *a="kammo DJ";
我需要用 C 语言编写一个 strcat。我尝试了以下操作: char * c_strcat( char * str1, const char * str2) { char * ret
我正在从文件中读取行,并且我决定按 char 逐个读取 char 中的部分内容。这是我得到的: char str[500]; // it has to be this size, I promi
这个问题已经有答案了: Can I copy a string in an empty string? (3 个回答) 已关闭 8 年前。 自学C语言充满惊喜。我做这个简短的片段来测试 strcat(
我是一名优秀的程序员,十分优秀!