与 Memcpy 连接
我正在尝试连接两个字符串,但无法使用 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第三次调用中的地址应为:
请注意,对于
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:
Note that for
T * p
, the expressionp[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 asi
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]
).您将最后一个数组元素的值赋予第二个 memcpy 。
正确的方法是给出地址(使用 & 运算符(就像第二个语句中隐含的含义一样)。
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).
你的代码示例有点可怕,但
应该可以工作,if l->db.param_key.param_name 和 l->userId_const 是一个字符数组。
your codeexample is a little bit horrible, but
should work, if l->db.param_key.param_name and l->userId_const are a char-arrays.
使用
memcpy()
与strcpy()
完全相同,只不过您必须使用字符串大小而不是字符串长度。Use
memcpy()
exactly likestrcpy()
except you have to work with string size instead of string length.