Yes, I remember staring at that code myself.  It's kind of grungy.

In:

        LibPackedDBRecord *packed=0;

"packed" is simply a pointer (32-bits for Palm platform) so this just
initializes it to zero.  Typically, you would say it as:

        LibPackedDBRecord *packed=NULL;

but the author (I think) is intentionally using zero instead of NULL.  Think
of *packed as pointing to a LibPackedDBRecord whose base address is zero.

Then the expression:

        &packed->status

is the offset of the "status" member from the base address of a
LibPackedDBRecord.  Note that &packed->status does not reference the value
of "status" but only yields the offset of "status".  DmWrite requires the
offset.

To me it's kind of kludgy.  I probably would code it differently, or at
least comment it better.

Hope this helps...

Dennis Leas
-----------
[EMAIL PROTECTED]



-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of
DongDong
Sent: Thursday, June 26, 2003 8:46 AM
To: Palm Developer Forum
Subject: Question on a routine


I'm reading the book "Palm OS Programming Bible" Chatper 10. I met some
question on the following routine. I've studied it again and again but I
don't know what missed.

Here is the question:
(A) 'packed' is declared as pointer to LibPackedDBRecord. Why it can be used
after just initialization to zero? Also, the fields of structure can be
accessed without assignment of address of structure to it. I'm confused.

Welcome to any advice.

======================== type definition in concern

typedef struct {
    LibStatusType status;
    LibDBRecordFlags flags;
    unsigned char lastNameOffset;
    unsigned char firstNameOffset;
    unsigned char yearOffset;
    unsigned char noteOffset;
    char firstField;
} LibPackedDBRecord;

======================== routine with question

static void PackRecord( LibDBRecordType *record, MemPtr recordDBEntry )
{
    UInt32 offset;
    Int16 index;
    UInt16 length;
    MemPtr p;
    LibDBRecordFlags flags;
    LibPackedDBRecord *packed=0;   <------------ (A)
    Char lastNameOffset=0, firstNameOffset=0,
        yearOffset=0, noteOffset=0;

    flags.allBits = 0;

    DmWrite( recordDBEntry, (UInt32) &packed->status,  <---------
        &record->status, sizeof( record->status ));
    offset = (UInt32)&packed->firstField;

    for (index=0; index<libFieldsCount; index++) {
        if (record->fields[index] != NULL ) {
            p = record->fields[index];
            length = StrLen(p) + 1;

            DmWrite( recordDBEntry, offset, p, length );
            offset += length;
            SetBitMacro( flags.allBits, index);
        }
    }
    .
    . <----- no 'packed' variable appears in-between
    .
    DmWrite( recordDBEntry, (UInt32)(&packed->lastNameOffset),  <-------
        &lastNameOffset, sizeof( lastNameOffset));
    DmWrite( recordDBEntry, (UInt32)(&packed->firstNameOffset),  <-------
        &firstNameOffset, sizeof(firstNameOffset));
    DmWrite( recordDBEntry, (UInt32)(&packed->yearOffset),  <-------
        &yearOffset, sizeof( yearOffset));
    DmWrite( recordDBEntry, (UInt32)(&packed->noteOffset),   <-------
        &noteOffset, sizeof(noteOffset));
}

========================



--
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/



-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to