- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
所以我的任务是:
Using the
strncpy
andstrncat
functions in#include<cstring>
, implement a functionvoid concat(const char a[ ], const char b[ ], char result[ ],
int result_maxlength)that concatenates the strings
a
andb
to the bufferresult
. Be sure not to overrun the result. It can holdresult_maxlength
characters, not counting the\0
terminator. (That is, the buffer hasbuffer_maxlength + 1
bytes available.) Be sure to provide a ‘\0’ terminator.
我的解决方案(到目前为止)如下,但我不知道自己做错了什么。当我实际运行程序时,我不仅收到运行时检查失败 2 错误,而且我不确定应该在哪里添加 \0
终止符,或者即使我应该使用 strncat
而不是 strncpy
.希望有人能引导我朝着正确的方向前进。是的,这是硬件。这就是为什么我说只要引导我朝着正确的方向前进,这样我就可以尝试弄明白 :p
#include <iostream>
#include <cstring>
using namespace std;
void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength);
int main()
{
char a[] = "Woozle";
char b[] = "Heffalump";
char c[5];
char d[10];
char e[20];
concat(a, b, c, 5);
concat(a, b, d, 10);
concat(a, b, e, 20);
cout << c << "\n";
cout << d << "\n";
cout << e << "\n";
return 0;
}
void concat(const char a[ ], const char b[ ], char result[ ], int result_maxlength)
{
strncat(result, a, result_maxlength);
strncat(result, b, result_maxlength);
}
最佳答案
至少在 concat
中的第一个 strncat
之前,您的 result
是未初始化的。
编辑:是的,正如 Michael Burr 指出的那样,您的结果大小应该随着您的进步而变化,并从一开始就进行计算。实际上,您选择的名称具有误导性,因为它是源的最大大小,而不是目标。
关于c++ - strncat 和 strncpy 帮助 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12217528/
我想连接两个字符串,添加一个新的随机字符,使用 strncat() 所以基本上我是这样做的: #include #include #include #define CHARACTER_RANGE
如何将 strncat 与堆对象一起使用? 我试图编写一个简单的函数来将 2 个字符串连接在一起并返回结果,但是,如果不使返回缓冲区非常大(增加大约 5000 的长度)以使其不会溢出,我就无法运行它。
如何将 strncat 与堆对象一起使用? 我试图编写一个简单的函数来将 2 个字符串连接在一起并返回结果,但是,如果不使返回缓冲区非常大(增加大约 5000 的长度)以使其不会溢出,我就无法运行它。
我正在尝试从目录中读取文件并在其他目录中的“相应”文件中写入一些信息。为了获取新文件的路径和名称,我使用 strncat 函数连接一些字符串。我的问题是,strncat 在某些字符串的开头添加了一些字
我有以下代码: struct prefix rnp; char prefix[IPV6_PREFIX_STR_MAX_LEN]; ... strncat(prefix, "/", 1); <----
很抱歉,如果这太入门级,但我尝试实现strcpystrncat()的库函数如下: #include void strncat (char *s, char *t, int n) { // mallo
我正在编写一个 Web 服务器,我在其中通过 WinSock 套接字从客户端接收数据,并将数据解析为多个部分。然后,根据找到的方法和请求的资源,我想构建一个新的数据包,我将把它发送回客户端。 我有这个
我发现 string.h 标准库中的 strncat 函数有一些奇怪的行为,希望得到一些帮助以了解发生了什么。 我的问题的症结在于我创建的一个名为 readLine 的函数,目的是将文件的行作为 ch
首先,我编写了一个简单的程序。 1 #include 2 #include 3 #include 4 5 int main() 6 { 7 char *buf
我试图在 C 编程中连接两个字符串。这是我的代码: #include #include #include int main(int argc, char const *argv[]) {
考虑这段代码: limit = sizeof(str1)-strlen(str1)-1; strncat(str1,str2,limit); 如果 str2 长度大于 limit,strncat Nu
更新:关于 char、signed char 还是 unsigned 的问题最终没有意义。在这种情况下使用 memcpy 更合适,因为它对字节不加区别地工作。 不可能是一个更简单的操作,但我似乎错过了
我的 stringNAdd 函数将复制 strncat(原始)。我不能接受数组作为参数,但可以接受指针。我想知道我的代码是否正确? 固定代码如下: #include #include using
我无法弄清楚为什么我的 g++ 编译程序在调用 strncat() 时出现段错误。 我一直在这个网站和一般的谷歌搜索上跳来跳去,发现了许多类似的问题,但还没有找到适合我的解决方案。这是更大代码的一部分
假设我有一个大小为 10 个字符的数组(memset 为 0),我将其作为目标传递给 strncat,并且在源代码中我传递一个长度为 20 个字符的字符串(空终止),现在我应该将“计数”传递为 10
抱歉,我现在修改了代码: #include #include #include void main() { int i=0; char** f=NULL;
我有一个缓冲区,我正在做很多 strncat。我想确保我永远不会溢出缓冲区大小。 char buff[64]; strcpy(buff, "String 1"); strncat(buff, "Str
我想通过使用 strtok 和 strncat 为每个单词添加字符串“ay”。但似乎有一个我找不到的地方有冲突。它只给了我输出的第一个词“Computeray”。帮助? #include #incl
我有一个程序可以在缓冲区中连接一些字符串。 我之前用过strncpy。在网上查看了一些使用 snprintf 而不是 strncat 的建议后,我切换到 snprintf。但是,我注意到这部分程序(字
我正在尝试编写必须实现库函数 strncpy、strncat 和 strncmp 版本的代码,但运行时出现 Abort trap: 6 错误。任何想法都非常感谢: #include #include
我是一名优秀的程序员,十分优秀!