--- prasad wrote:
> Hi,
> Here I am getting problem with StrCat(); function
> whenever I am using this Function I am getting the Error
> like
> " Null String passed"
> I am sending tha code here please go through.
You seem to have some very basic misunderstandings about C and also
some misunderstandings about the Palm OS API. I will point out a few
of the problems below.
>
> I am declare the "errBuff" as global variable
> char *errBuff[50];
This means that errBuff is an array of 50 items, each item is a pointer
to a char. This does not allocate any memory to hold any strings of
text. It just allocates 50 pointers.
> static void MainFormInit()
> {
>
> FormPtr frm;
> ListPtr lst;
> UInt numRecords;
> frm=FrmGetActiveForm();
> numRecords=DmNumRecords(DrugDB);
> int recnum = numRecords/40;
> CharPtr tempStr,test;
This declares 2 pointers to Char (CharPtr is the same as Char *).
Again, it does not allocate any memory to store any strings.
> char *test1[50];
This declares 50 pointers to char, but does not allocate memory to hold
any strings.
> int i,j=0,tempInt,xx=0;
> int xx0=1;
> Handle recHandle1;
> CharPtr recText1,str1;
This declares 2 pointers to Char. Again, no memory to store any
strings.
>
> lst=(ListType
> *)FrmGetObjectPtr(frm,FrmGetObjectIndex(frm,MainUnnamed1001List));
>
> for(i=0;i<recnum;i++)
> {
>
> //Retrive information from database
> recHandle1=(char **)DmGetRecord(DrugDB,xx0);
Here you are assuming that a Handle is the same as (char **) this is
not a safe assumption. On the Palm OS, you lock a handle to get a
pointer. DmGetRecord returns a MemHandle; you should not cast it to
another data type.
> recText1=(char *)MemHandleLock(recHandle1);
> str1=recText1;
> MemHandleUnlock(recHandle1);
Here you lock recHandle1 to get a pointer to its memory chunk, then you
copy that address to str1, then you unlock the handle. Why? As soon
as you unlock the handle, the OS is free to move the memory around. If
it does move the memory, then the pointer will be worthless. If you
want to save the string that it points to, use StrCopy(). However, you
can't copy a string to a pointer without first allocating some memory.
For example, the following won't work:
#define msg "this is bad"
Char * str;
StrCopy(str, msg); // no memory to hold the string
but you can do this:
#define msg "this is ok"
Char * str;
str = MemPtrNew(StrLen(msg)+1);
StrCopy(str, msg); // ok
// only when you are done with the string:
MemPtrFree(str);
There are several other ways to do this, but the important point to
remember is: get a book on C and learn about pointers and memory, and
then read the docs about MemPtrNew/MemPtrFree,
MemHandleNew/MemHandleFree, and MemHandleLock/MemHandleUnlock before
you attempt to write C programs for the Palm OS.
> DmReleaseRecord(DrugDB,xx0, false);
Now that you have released the record, the odds are even higher that
the pointer to the string (str1) will be worthless!
>
> items1[j]=str1;
I don't see any declaration of the items1 array.
> StrCopy(errBuff[j],", ");
This will be a bad thing. What does errBuff[j] point to? Where is
there room to store ", "?
> StrCat(errBuff[j],str1);
More of the same problem.
> ErrDisplay(errBuff[j]);
> j=j+1;
> xx0=xx0+40;
> }
> LstSetListChoices(lst,errBuff,recnum);
> LstDrawList(lst);
> }
>
> any one have idea please tell me.
>
__________________________________________________
Do You Yahoo!?
Yahoo! Sports - Coverage of the 2002 Olympic Games
http://sports.yahoo.com
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/