--- Santosh Vernekar <[EMAIL PROTECTED]> wrote:
> On 12/28/06, Ray Devore <[EMAIL PROTECTED]>
> wrote:
> >   --- Santosh Vernekar <[EMAIL PROTECTED]
> <santosh.ver%40gmail.com>>
> > wrote:
> > > Ya i know... I have to answer some of these
> > > questions for the folks
> > > at a very crude level!!
> > > My perspective is just to give a visual help in
> > > understanding structures
> > > and the reason we use them, at a very basic
> level.
> > > Of course the example
> > > I gave wont work on all systems, for eg: it will
> > > break on 64bit systems
> > > having
> > > LP64 data model where the compiler may optimize
> and
> > > pad bytes along with
> > > the integer field mentioned in the structure to
> make
> > > it dword aligned.
> > > Anyways, it is just for beginners to get a brief
> > > understanding of it.
> > > Apologies
> > > for others who are bugged by the crude level of
> my
> > > solution!!
> > >
> > > Regards,
> > > Santosh.
> > >
> > > --
> > >
> > > Santosh Vernekar
> > > Sr. Dev. Engineer
> > > Calsoft PVT. LTD.
> > >
> > > On 12/28/06, Paul Herring
> <[EMAIL PROTECTED]<pauljherring%40gmail.com>
> > >
> > > wrote:
> > > >
> > > > On 12/28/06, Santosh Vernekar
> > > <[EMAIL PROTECTED]
> <santosh.ver%40gmail.com>> wrote:
> > > > >
> > > > > You might want to try understanding how to
> > > access different fields
> > > > > of a structure.
> > > > > Every structure can be visualized like a
> block
> > > of memory and accessing
> > > > > fields is like
> > > > > accessing the block at different offsets
> > > depending on the sizes of the
> > > > > fields.
> > > > > Hence ..
> > > > > typedef struct { *char
> > > **ch;
> > > > > *int* id;
> > > > > *float* salary;
> > > > > } Base;
> > > > >
> > > > > Base orgin;
> > > > >
> > > > >
> > > > > origin + 0 (Start Address) point to the
> first
> > > element (char *ch)
> > > > > origin + (sizeof char *) points to the
> second
> > > element in the block
> > > > > (int id)
> > > > >
> > > >
> > > > Not necessarily, and unless you specify
> certain
> > > options in the compiler,
> > > > not ususally.
> > > >
> > > > --
> > > > PJH
> > > >
> > > Santosh Vernekar
> > > Sr. Dev. Engineer
> > > Calsoft PVT. LTD.
> > >
> >
> > Santosh,
> >
> > Can you provide some actual code to show that your
> > example will work? Please include compiler, and
> > operating system tat you are using to get your
> example
> > to work.
> >
> > Ray
> >
> 
> Hi,
>     Here is a sample code you can try out. Again I
> emphasize this is just a
> crude
> solution for beginners to understand how memory is
> aligned for different
> fields of
> a data-structure.
> 
=============================================================
> #include <stdio.h>
> 
> typedef  struct {
>     char  *ch;
>     int  id;
>     float  salary;
> } Base;
> 
> int main()
> {
>     Base origin;
>     char *addr = NULL;
> 
>     addr = (char *)(&origin);
>     addr = (char *)(addr + sizeof(char *));
> 
>     *((int *)addr) = 4;
>     printf("\n origin.id=%d", origin.id);
>     printf("\n (addr)=%u", (addr));
>     printf("\n (addr + sizeof(char *))=%u", (addr +
> sizeof(char *)));
>     printf("\n (&origin)=%u", (&origin));
>     printf("\n (sizeof(char *))=%u", (sizeof(char
> *)));
>     printf("\n origin=%u\n origin.ch=%u\n
> origin.id=%u\n origin.salary=%u\n
> ",
>             &origin, &origin.ch, &origin.id,
> &origin.salary);
> 
>     return 0;
> }
===============================================================
> -- 
> Santosh Vernekar
> Sr. Dev. Engineer
> Calsoft PVT. LTD.
> 
Santosh,

Your example does not use the origin+... that your
earlier message stated.  It does answer the OP's
question, just not how your earlier message stated it
would work.

Thanks for the example.
Ray

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply via email to