memcpy(msg->data+msg->dsize, ..)
memcpy function is used for copy to dst from src with size:
Code:
memcpy(dst, src, size)
If msg structure was defined as follow:
Code:
struct msg{
char data[100];
int dsize;
}
and buf defined as follow:
Code:
struct buf{
char *data;
int len;
}
Now if i run below command:
Code:
memcpy(msg->data+msg->dsize, buf->data, buf->len)
Does copy buf->data to msg->data? If yes, what is effect of msg->dsize?
I saw this in a C project.
Re: memcpy(msg->data+msg->dsize, ..)
I just wrote a program for copying the strings in the structure,
If you want to copy the whole structure you can use the size of the structure in the memcpy.
It will copy that many number of bytes from one structure to another.
Code:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct msg{
char data[100];
int dsize;
};
struct buf{
char data[100];
int len;
};
int main()
{
char str1[]="hello";
char str2[10];
struct msg *m1;
struct buf *b1;
b1=(struct buf *)malloc(sizeof(struct buf));
m1=(struct msg *)malloc(sizeof(struct msg));
strcpy(b1->data,"hello");
b1->len=strlen(b1->data);
// m1->dsize=0;
memcpy(str2,str1,10);
memcpy(m1->data, b1->data, sizeof(struct buf));
printf("str1:%s str2:%s\n",str1,str2);
printf("str1:%s size:%d str2:%s size:%d \n",b1->data,b1->len,m1->data,m1->dsize);
return 0;
}