memcpy() 的用法
自从我玩C这两天以来,我一直遇到新问题。现在我想将我的数据memcopy到char buf中,但我遇到了一些问题:我的代码如下:
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
unsigned char buf[1024];
struct profile_t
{
unsigned char length;
unsigned char type;
unsigned char *data;
};
typedef struct profile_datagram_t
{
unsigned char src[4];
unsigned char dst[4];
unsigned char ver;
unsigned char n;
struct profile_t profiles[MAXPROFILES];
} header;
header outObj;
outObj.src[0] =1;
outObj.dst[0] = 2;
outObj.ver = 1;
outObj.n = 2;
outObj.profiles[0].length = 10;
outObj.profiles[0].type = 1;
outObj.profiles[1].length = 10;
outObj.profiles[1].type = 2;
memcpy(buf,outObj.src,4);
memcpy(buf+4,outObj.dst,4);
memcpy(buf+8,outObj.ver,1);
memcpy(buf+9,outObj.n,2);
memcpy(buf+10,outObj.profiles[0].length,1);
memcpy(buf+11,outObj.profiles[0].type,1);
memcpy(buf=12,outObj.profiles[0].data,10);
我收到以下错误:
警告:传递
分配给类型memcpy
的参数 2 使指针来自整数而不进行强制转换
错误:从类型int
unsigned char[1024]
时,类型不兼容
该错误针对 memcpy()
。谁能向我解释一下 memcpy()
以及我哪里出错了。
Since two days I am playing with C and I come across new problem all the time. Right now I want to do memcopy my data into a char buf, but I am encountering some problems: My code is below:
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
unsigned char buf[1024];
struct profile_t
{
unsigned char length;
unsigned char type;
unsigned char *data;
};
typedef struct profile_datagram_t
{
unsigned char src[4];
unsigned char dst[4];
unsigned char ver;
unsigned char n;
struct profile_t profiles[MAXPROFILES];
} header;
header outObj;
outObj.src[0] =1;
outObj.dst[0] = 2;
outObj.ver = 1;
outObj.n = 2;
outObj.profiles[0].length = 10;
outObj.profiles[0].type = 1;
outObj.profiles[1].length = 10;
outObj.profiles[1].type = 2;
memcpy(buf,outObj.src,4);
memcpy(buf+4,outObj.dst,4);
memcpy(buf+8,outObj.ver,1);
memcpy(buf+9,outObj.n,2);
memcpy(buf+10,outObj.profiles[0].length,1);
memcpy(buf+11,outObj.profiles[0].type,1);
memcpy(buf=12,outObj.profiles[0].data,10);
I am getting the following errors:
warning: passing argument 2 of
memcpy
makes pointer from integer without a cast
error: incompatible types when assigning to typeunsigned char[1024]
from typeint
The errors are for memcpy()
. Can anyone explain me about memcpy()
and also where I am going wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要传递结构的各个字段的地址。当您按名称传递数组时,该名称将自动成为数组开头的地址。但非数组字段需要地址运算符
&
:顺便说一句,如果您更改
length
的类型,这会更好:You need to pass the address of the various fields of your structures. When you pass an array by name, that is automatically the address of the beginning of the array. But the non-array fields need the address-of operator
&
:As an aside, this is better in case you change the type of
length
:我将讨论两个主要问题:
您需要使用
ver
或n
等结构中非指针项的地址:您不小心分配了最后一行中
buf
的值:There are two main problems that I'll touch on:
You'll need to use the address of the non-pointer items in the structures like
ver
orn
:You accidentally assign a value to
buf
in the last line:作为提示,如果您确定您的
buf
的内存布局和字节顺序以及结构(包括其填充和对齐),您可以简单地进行强制转换像:并自动访问成员。如
header->ver
、header->src
等Just as a tip, if you are sure about the memory layout and endianess of your
buf
, and the struct (including its padding, and alignment) you could simply do a cast like:And automagically access the members. Like
header->ver
,header->src
, etc