Nathan Fisher wrote:
The part of code that is causing the error is when I'm trying to create a new Handle using "DmNewHandle". It seems to fail. The compile error I get is "Illegal implicit conversion from 'void *' to "UserRecord *'". Which means that the Handle "UserRecordH" must be void and therefore wasn't created properly.

In the C programming language, the compiler keeps track of the type of
different variables, so that it can prevent mistakes like this:

        int i = 42;
        UserRecord ur;

        ur = i;

Obviously, it doesn't make any sense to assign 42 to a UserRecord, because
the variables are different types.  So the compiler knows right away this
statement is nonsense and it won't even try to compile it.  This usually
saves the programmer some heartache.

In C, this type checking extends to pointers.  The compiler knows that, just
like it can't assign an integer to a UserRecord, it isn't safe to assign
a pointer-to-an-integer to a pointer-to-a-UserRecord.  And you are trying
to do something like that, which is why you get the "illegal implicit
conversion" error message.

Now, to be more specific about your situation, a "void *" is a pointer to
void.  This simply means that the pointer exists, and it points to something
(or at least it could), but at that point in the program, the compiler
doesn't know what KIND of thing it points to.  Basically, a void pointer
is a "pointer to an i-don't-know-what".

So, why are you getting that message?  When you do a MemHandleLock(), that
function is locking some memory, but C doesn't have a way for it to return
different pointer types based on what type the data is that's contained
in the memory that MemHandleLock() is locking.  (Some other languages do
have ways to keep track of types and pass that information through function
calls, but C avoids this for the sake of efficiency.)

So basically, if you do a MemHandleLock() and you know that the memory
you're locking actually contains a UserRecord, then the C compiler doesn't
know this, so you have to tell the compiler's type checking system this
by doing a typecast:

        UserRecord *ur;

        ur = (UserRecord *) MemHandleLock (myhandle);

That basically says to the compiler, "Hey, by the way (hence the parentheses),
you didn't know it, but this pointer value is actually a 'UserRecord *'."

So, if you wanted to do that, you would need to use a typecast.

But in your specific situation, there is an easier way.  MemHandleLock()
returns a void pointer, and DmWrite() accepts a void pointer.  So, you
could just change the type of the UserRecordP variable to void * instead
of UserRecord *.  You never actually use UserRecordP to access the members
of the structure, so the compiler doesn't need to know that that pointer
points to that type of data.  In order to get the pointer's value from
MemHandleLock() and pass it to DmWrite(), the compiler just needs to know
that it's some unspecified kind of pointer, which is sorta the purpose of
void pointers in the first place.

By the way, you have one other bug in your function.  You declare a
variable called UserRecord up near the top of the OpenDatabase() function,
but you never write anything into it, which means it will contain garbage.
But then while it still contains garbage, you are copying from it to
the storage heap with DmWrite().  So, you are writing garbage into the
database.  :-)

  - Logan

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

Reply via email to