Return-Path: william@bourbon.usc.edu Delivery-Date: Sat Sep 6 08:58:43 2008 X-Spam-Checker-Version: SpamAssassin 3.2.3 (2007-08-08) on merlot.usc.edu X-Spam-Level: X-Spam-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.2.3 Received: from bourbon.usc.edu (bourbon.usc.edu [128.125.9.75]) by merlot.usc.edu (8.14.1/8.14.1) with ESMTP id m86FwhOD022320 for ; Sat, 6 Sep 2008 08:58:43 -0700 Received: from bourbon.usc.edu (localhost.localdomain [127.0.0.1]) by bourbon.usc.edu (8.14.2/8.14.1) with ESMTP id m86FvEJX024135 for ; Sat, 6 Sep 2008 08:57:14 -0700 Message-Id: <200809061557.m86FvEJX024135@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: Problem using memcpy and send function Date: Sat, 06 Sep 2008 08:57:14 -0700 From: Bill Cheng Someone wrote: > I have a doubt in using memcpy. > I have a structure > > typedef struct tagReqMsg { > unsigned short MsgType; > > unsigned int Offset; > unsigned int DataLen; > > char *Data; > } ReqMsg; > > > ReqMsg *request; > char *msg_buf = (char*)malloc(10); ...for eg. > > memcpy(msg_buf,*request.MsgType,2); > memcpy(msg_buf[2],*request.Offset,4); > memcpy(msg_buf[6],*request.DataLen,4); > > The memcpy statement gives me a segmentation fault. > How can I copy the contents of pointer to a structure into a char buffer? Did you turn on compiler warning (-Wall) when you compile? Please try to fix the warning and if you still don't know how to fix your code, please ask me again. > Second question, > > In the send function how can I send a char? > for eg. > while(msg_buf[index] != '\0'){ > send(sockfd, msg_buf[index], 1, 0) ; > index++; > }. > > The above code breaks at the send function giving some argument mismatch > error. The 2nd argument for send() should be compatible with (const char *) and it should be a memory location and not a character. msg_buf[index] is a character (or unsigned char, depending on how you define msg_buf). You need the address of this character as a starting point of a memory buffer. So, you should use put an ampercent in front of it, i.e.: &msg_buf[index] -- Bill Cheng // bill.cheng@usc.edu