- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
int len ;
strcat( str1, str2);
len = strlen(str1);
cout << len << endl<<str1<<endl;
return 0;
}
当我像这样初始化 str1 时 char str1[5]="hello";
,C++ 编译器给我一个错误(意味着字符串的大小必须比给定的字符串大 1)。但在 strcat
中,我们将 str1
与 str2
连接在一起,第一个字符串的大小为 10,字符串的长度也为 10(表示没有额外的空字符)。为什么这种行为会发生变化?我们在strcat
函数中是否将C++ 字符串转换为C 字符串,请解释。
最佳答案
Are we converting c++ string
不,您的程序中没有涉及 C++ 字符串(即 std::string
)。
why is this changed behavior ?
你写了一个不同的程序,行为是不同的。
在第一种情况下,您的程序格式错误。该标准要求编译器在编译时通知您这一点。编译器也很容易检测到你的错误。变量的类型是“5 个字符的数组”。 6 个字符不适合 - 类型错误。类型在编译时已知。
在示例代码中,您使用了 strcat
。如果你看一下this reference你会发现
The behavior is undefined if the destination array is not large enough for the contents of both src and dest and the terminating null character.
标准不要求编译器警告您未定义的行为。事实上,编译器通常很难证明程序具有未定义的行为 - 即使在这种特殊情况下它看起来很明显。
strcat
的参数是指针。您将正确类型的指针传递给 strcat
。没有类型错误。该错误存在于错误的值中。这些值在运行时被复制到数组中(除非优化器做了一些魔术)。为了找出你的错误,编译器必须模拟程序的执行。对所有代码路径执行此操作将非常缓慢。因此,编译器将满足 strcat
前提条件的责任留给了程序员。
关于c++ 字符串行为在定义和在 strcat 函数中是不同的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40085108/
这个问题在这里已经有了答案: 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(
我是一名优秀的程序员,十分优秀!