> According to the example of UnpackCustomer shown on the book of Palm
> Programming,
>
> the function prototype of UnpackCustomer is :
> static void UnpackCustomer(Customer *customer, const PackedCustomer
> *packedCustomer)
>
> However the voidPtr to the locked record is passed as the second parameter.
> Even so, packedCustomer->customerID and packedCustomer->name is still
> valid. Is it because of the fact that every element in a structure is
> written consecutively and in the order specified in the structure
> declaration?
>

I think what the authors intended was the following:

A packed customer (in a database record) is stored as a 4-byte ID, followed
by 4 null-terminated strings (name, address, city, phone).

Here's the declaration

typedef struct {
    SDWord customerID;
    char    name[1];    // actually longer than 1
} PackedCustomer.

Here's an example of the bytes of a (small) packed customer:
    0x00 0x00 0x00 0x52 'N' 'e' 'i' 'l' '\0' 'M' 'a' 'i' 'n' '\0' 'R' 'e'
'd' 'l' 'a' 'n' 'd' 's' '\0' '7' '9' '3' '-' '5' '9' '9' '5' '\0'

Notice that the name will always start at offset 4 within the packed record.

An unpacked customer is used in memory, to actually access the name,
address, city, and phone.

typedef struct {
    SDWord    customerID;
    const char *name;
    const char *address;
    const char *city;
    const char *phone;
} Customer;

It has four character pointers which point into the appropriate offsets
within the associated (locked) PackedCustomer.

If the example packed customer above were at memory address 0x1230, then the
Customer would be
    0x00000052        // id
    0x1234            // offset 4 within packed customer
    0x1239            // offset 9 within packed customer
    0x123e            // offset 14 within packed customer
    0x1247            // offset 23 within packed customer

The authors should have made it more clear in the book, but I think they
(incorrectly) assumed this was a well-known, or obvious technique.


Neil

Reply via email to