与 Memcpy 连接

发布于 2024-11-27 13:30:22 字数 435 浏览 2 评论 0原文

我正在尝试连接两个字符串,但无法使用 strcpy 和 strcat,因此我尝试通过 memcopy 来完成此操作。然而,在第三个语句中,memcpy 并没有添加到第一个 memcpy 的延续中。知道如何做到这一点吗?

memset(&l->db.param_key.param_name, ' ', sizeof(l->db.param_key.param_name));
memcpy(l->db.param_key.param_name,g->program_id_DB,(strlen(g->program_id_DB)));
memcpy(l->db.param_key.param_name[strlen(g->program_id_DB)+1],l->userId_const,sizeof(l->userId_const));

I'm trying to concatenate two string and I cannot use strcpy and strcat, so I'm trying to do this through memcopy. However, on the third statement the memcpy it is not adding on to the continuation of the first memcpy. Any idea how to do this?

memset(&l->db.param_key.param_name, ' ', sizeof(l->db.param_key.param_name));
memcpy(l->db.param_key.param_name,g->program_id_DB,(strlen(g->program_id_DB)));
memcpy(l->db.param_key.param_name[strlen(g->program_id_DB)+1],l->userId_const,sizeof(l->userId_const));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾听心声的旋律 2024-12-04 13:30:22

第三次调用中的地址应为:

l->db.param_key.param_name + strlen(g->program_id_DB) + 1

请注意,对于 T * p,表达式 p[i];*(p + i)< 相同/代码>。您不想取消引用,您想要地址,因此只需添加到指针即可。

只要 i 是有效索引,p + i 就与 &p[i] 相同。)

( 请注意@Nobody 的观察,您的第一行不正确,您应该只说 l->db.param_key.param_name (或等效的&l->db.param_key.param_name[0])。

The address in the third invocation should be:

l->db.param_key.param_name + strlen(g->program_id_DB) + 1

Note that for T * p, the expression p[i]; is identical to *(p + i). You don't want to dereference, you want the address, so you just add to the pointer.

(It is also true that p + i is identical to &p[i] as long as i is a valid index.)

Also mind @Nobody's observation that your first line is incorrect and you should just say l->db.param_key.param_name (or equivalently &l->db.param_key.param_name[0]).

剩一世无双 2024-12-04 13:30:22

您将最后一个数组元素的值赋予第二个 memcpy 。
正确的方法是给出地址(使用 & 运算符(就像第二个语句中隐含的含义一样)。

memcpy(&l->db.param_key.param_name[strlen(g->program_id_DB)+1],l->userId_const,sizeof(l->userId_const))

You are giving to the second memcpy the valye of the last array's element.
The correct way is to give the address(with the ampersand operator (like it was implicitly meant in the second statement).

memcpy(&l->db.param_key.param_name[strlen(g->program_id_DB)+1],l->userId_const,sizeof(l->userId_const))
箜明 2024-12-04 13:30:22

你的代码示例有点可怕,但

memset(l->db.param_key.param_name,0,sizeof(l->db.param_key.param_name));
memcpy(l->db.param_key.param_name,g->program_id_DB,strlen(g->program_id_DB));
memcpy(&l->db.param_key.param_name[strlen(g->program_id_DB)],l->userId_const,sizeof(l->userId_const));

应该可以工作,if l->db.param_key.param_name 和 l->userId_const 是一个字符数组。

your codeexample is a little bit horrible, but

memset(l->db.param_key.param_name,0,sizeof(l->db.param_key.param_name));
memcpy(l->db.param_key.param_name,g->program_id_DB,strlen(g->program_id_DB));
memcpy(&l->db.param_key.param_name[strlen(g->program_id_DB)],l->userId_const,sizeof(l->userId_const));

should work, if l->db.param_key.param_name and l->userId_const are a char-arrays.

熟人话多 2024-12-04 13:30:22

使用 memcpy()strcpy() 完全相同,只不过您必须使用字符串大小而不是字符串长度。

Use memcpy() exactly like strcpy() except you have to work with string size instead of string length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文