Return-Path: william@bourbon.usc.edu Delivery-Date: Sat Sep 6 20:19:15 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 m873JEr4028924 for ; Sat, 6 Sep 2008 20:19:15 -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 m873HrvK031090 for ; Sat, 6 Sep 2008 20:17:53 -0700 Message-Id: <200809070317.m873HrvK031090@bourbon.usc.edu> To: cs551@merlot.usc.edu Subject: Re: how to obtain the address of the individual members of the structure... Date: Sat, 06 Sep 2008 20:17:53 -0700 From: Bill Cheng Someone wrote: > I want to write a function so that i can avoid writing the following > statements again and again > > memcpy(msg_buf, &clientRequestMsg.msgType, 2); > memcpy(&msg_buf[2], &clientRequestMsg.offset, 4); > memcpy(&msg_buf[6], &clientRequestMsg.dataLen, 4); > > Therefore, I wrote a function > > void StructureToArray (char *ptr3, struct message* ptr4) > { > memcpy(ptr3, (ptr4->msgType), 2); > memcpy(ptr3[2],(ptr4->offset), 4); > memcpy(ptr3[6], (ptr4->dataLen), 4); > } > > now obviously, the above function wont work because (ptr4->msgType) will > give me the value of msgType and not its address.............so how do i > obtain the address of the individual members of the structure........... You should use: &(ptr4->msgType). Also, ptr3[2] has data type (char) and you need (char*). So, you should use &ptr3[2]. -- Bill Cheng // bill.cheng@usc.edu