memcpy() 的用法

发布于 2024-11-26 14:37:36 字数 1394 浏览 3 评论 0原文

自从我玩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 type unsigned char[1024] from type int

The errors are for memcpy(). Can anyone explain me about memcpy() and also where I am going wrong.

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

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

发布评论

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

评论(3

野稚 2024-12-03 14:37:36

您需要传递结构的各个字段的地址。当您按名称传递数组时,该名称将自动成为数组开头的地址。但非数组字段需要地址运算符 &

memcpy(buf+10,&outObj.profiles[0].length,1);

顺便说一句,如果您更改 length 的类型,这会更好:

memcpy(buf+10,&outObj.profiles[0].length,sizeof(outObj.profiles[0].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 &:

memcpy(buf+10,&outObj.profiles[0].length,1);

As an aside, this is better in case you change the type of length:

memcpy(buf+10,&outObj.profiles[0].length,sizeof(outObj.profiles[0].length));
哭泣的笑容 2024-12-03 14:37:36

我将讨论两个主要问题:

  • 您需要使用 vern 等结构中非指针项的地址:

    /* 注意 &在 outObj.ver 之前 */
    memcpy(buf+8, &outObj.ver, 1);
    
  • 您不小心分配了最后一行中 buf 的值:

    memcpy(buf=12,... /* 应该是 buf+12 */
    

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 or n:

    /* note the & before outObj.ver */
    memcpy(buf+8, &outObj.ver, 1);
    
  • You accidentally assign a value to buf in the last line:

    memcpy(buf=12,... /* should be buf+12 */
    
向地狱狂奔 2024-12-03 14:37:36

作为提示,如果您确定您的 buf 的内存布局和字节顺序以及结构(包括其填充和对齐),您可以简单地进行强制转换像:

header * outObj = (header *)buf;

并自动访问成员。如header->verheader->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:

header * outObj = (header *)buf;

And automagically access the members. Like header->ver, header->src, etc

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