Well, you're doing a few things which are probably not exactly "the right 
thing".

First, "PackedClient *packedClient" declares a pointer to a PackedClient.
It does NOT dereference that pointer.

Second, MemHandleLock(h) returns a ptr, you can't cast a ptr to a structure,
which you're trying to do.

To do what you want to do, you should write code like this:

    h = DmQueryRecord(gClientsDB, index);
    if (h) {
        PackedClient pc;

        pc = *((PackedClient *)MemHandleLock(h));

        /* Be sure to finish using pc within this if statement,
           though.
        */
    }

Which brings me to the third thing.  You really don't want to do that.  What
you really want is to do something more like this:

    h = DmQueryRecord(gClientsDB, index);
    if (h) {
        PackedClient pc;

        MemMove(&pc, MemHandleLock(h), sizeof(pc));
        MemHandleUnlock(h);

        /* Be sure to finish using pc within this if statement,
           though.
        */
    }

The reason is that a structure assignment will not necessarily get you an
exact copy of the memory that you're trying to copy.  95% of the time, it
won't matter, until one day it bites you in the butt.  Structure assignment
is usually best avoided in C.  (If you're writing C++, you've got a whole
different set of issues with structure assignment.)

For that matter, don't cast structures.  Cast structure pointers.  More
often than not, it's better.  (Again, C++ is a different beast.)

Also, be sure that you are done with pc by the time you leave the if
statement.  Otherwise, pc will fall out of scope, and it will fail to
compile.

Good luck with your project.

George




----------
>From: "Colletti, James E" <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Casting to structures
>Date: Wed, Oct 13, 1999, 16:22
>

> If anyone could shed some light on this, I'd appreciate it
>
> I have a database record that I'm trying to read using DmQueryRecord.  I
> receive a VoidHand pointer and am trying to cast it to my structure type.
> For example:
>
>  typedef struct
>  {
>   SDWord clientID;
>   SDWord age;
>   char name[1];
>   } PackedClient;
>  
>  static void test()
>  {
>   h = DmQueryRecord(gClientsDB,index);
>  
>   if (h)
>   {
>    PackedClient *packedClient = (PackedClient)
> MemHandleLock(h);
>   }
>  }
>
> When I try this casting, I get a compile error specifing such void ** to
> PackedClient casting is illegal.  I've seen such casting performed many
> times in O'Reilly's Palm Programming book as well as in other literature.
> What am I leaving out?
>
> Thanks
>
> James Colletti
> Retail Branch Systems
> Salomon Smith Barney
> 212-723-3256
>
> 

Reply via email to