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
memcpymakes 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.
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: