From: Akshat Aranya Subject: Re: code explaination Date: Thu, 18 Sep 2003 11:13:02 -0400 (EDT) Sender: linux-fsdevel-owner@vger.kernel.org Message-ID: References: <20030918093728.14669.qmail@mailweb34.rediffmail.com> Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Cc: linux-fsdevel@vger.kernel.org Return-path: Received: from sbcs.sunysb.edu ([130.245.1.15]:59620 "EHLO sbcs.cs.sunysb.edu") by vger.kernel.org with ESMTP id S261458AbTIRPNk (ORCPT ); Thu, 18 Sep 2003 11:13:40 -0400 To: vadiraj c s In-Reply-To: <20030918093728.14669.qmail@mailweb34.rediffmail.com> List-Id: linux-fsdevel.vger.kernel.org The output of abc_char depends on the platform you're running it on. When you type cast your structure to a char, you're effectively accessing the lowest byte of pqr.i. On x86, which is little endian, this will contain the value 100 (0x64). On a big endian machine this will be zero. When you access pqr.i = 300, the lowest byte will be 44 (0x2c) on little endian and zero on big endian. Two more points: 1. Writing such code is a Bad Thing, since it relies on the endianness of the platform, and the way the compiler pads structures. 2. This discussion does not belong to linux-fsdevel, since it is a very basic C question and is in no way relevant to Linux F/S development. Regards, Akshat On Thu, 18 Sep 2003, vadiraj c s wrote: > > Please tell me the reason for the out put of the code > > > #include > struct xyz{ > > int i; > int j; > } pqr = {100,300} ; > int main() > { > struct xyz *z = &pqr; > abc_char(z); > abc_int(z) ; > } > > void abc_char(char *i) > { > printf("char abc %d",*i) ; > } > > void abc_char(int *i) > { > printf("int abc %d",*i) ; > } > > out put: > char abc 0 > int abc 300; > swap the structure value to i=300,j = 100; > you get > char abc 1; > > can you type cast structure add to any datatype? > what can be the reason for such output; > > regards > vadiraj