- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
让我们举个例子。
#include <stdio.h>
#include <string.h>
int main() {
char str1[7] = "hello ";
printf("Initial size of str1 is: %d\n", strlen(str1));
char str2[] = "buddy";
printf("%s\n", strcat(str1, str2));
printf("Final size: %d\n", strlen(str1));
}
上述程序的输出将是
Initial size of str1 is: 6
hello buddy
Final size: 11
--------------------------------
Process exited after 0.835 seconds with return value 0
Press any key to continue . . .
看到了吗? str1
的大小如何从 7 变为 11(包括 null 变量),关于我认为会发生的事情是:
str1
从与以前相同的地址开始重新分配了连续内存,即 str1
大小为 strlen(str1)+strlen( str2)+1
+1 为空值,然后重新定义它得到hello buddy。如果我错了,请告诉我,如果不是,那它是什么功能,它是如何工作的?
还有一个问题:如何在不使用 strcat
函数的情况下编写代码来完成上述任务。
我尝试使用 realloc()
来做,但没有完全成功,可能是因为 realloc()
只能重新分配动态分配的内存,是这样吗?
最佳答案
缓冲区溢出
OP 的代码失败,因为 strcat(str1,str2)
试图写入超过 str1[]
的结尾 - 结果:未定义的行为 ( UB)。 @dimich
改为使用更大的目标缓冲区。
// char str1[7]="hello ";
char str1[7 + 5]="hello ";
char str2[]="buddy";
printf("%s\n",strcat(str1,str2));
使用正确的打印说明符
strlen()
返回一个 size_t
,而不是一个 int
。
// printf("Initial size of str1 is: %d\n",strlen(str1));
printf("Initial size of str1 is: %zu\n",strlen(str1));
提示:启用所有警告。
备选
许多备选方案之一:将 str2
复制到 str1
的末尾。
// printf("%s\n",strcat(str1,str2));
strcpy(str1 + strlen(str1), strt2);
printf("%s\n",str1);
realloc()
realloc()
can only reallocate dynamically allocated memory, is it so?
realloc()
不应用于指向未分配、非NULL
指针的指针。
除了重新分配动态分配的内存之外,realloc()
可以在没有预先分配的情况下启动。
char *p = realloc(NULL, size);
// just like
char *p = malloc(size);
故事的寓意
关于c - strcat() 函数实际上是如何工作的及其替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73204284/
这个问题在这里已经有了答案: 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(
我是一名优秀的程序员,十分优秀!