Two problems:
You are not allocating any memory for "theConvertDate".
You are pointing it to the location of the string constant: "12/09/79".

As for allocating the memory, you must make a choice: do you
want to require the calling function to deallocate the memory or
not?  (Probably not for a function like this.)

If you -do- want the caller to be responsible, declare theConvertDate
this way:

    CharPtr theConvertDate = MemPtrNew(20);

If you do -not- want the caller to bre responsible for deallocating the
memory, declare this:

    Char theConvertDate[20];

Note that 20 should be more then long enough for a date, but it
is still a number I just picked out of the air.  If we are certain that
dates
will always be "mm/dd/yy" then 9 would be enough ... but if we
use 9 that would break on "mm/dd/yyyy" formats, which would
require a size of 11.  Then there is "dd-mmm-yyyy" and other
such variants ... you are welcome to use your own judgement
on what size you need.

Now, as for the initialization you performed, you don't want
to assign the address of the constant to your pointer, you want
to copy that content into the memory allocated for theConvertDate.
This is done with StrCopy().

    StrCopy(theConvertDate, "12/08/79");

Now all of the above is essentially basic C programming and
has nothing to do with the Palm platform in particular.  I would
recommend that you find yourself a good intro-to-C book and
work through all the excercises, especially those concerned
with dynamic memory allocation and string handling.


--
-Richard M. Hartman
[EMAIL PROTECTED]

186,000 mi/sec: not just a good idea, it's the LAW!


Eliah Ninyo wrote in message <32671@palm-dev-forum>...
>
>hello,
>
>i wrote a function that need to return the current device date. its return
a
>string (Charptr).
>here is the function:
>
>static CharPtr SetAutoDate ( void )
>{
>     DatePtr  the_date = NULL;
>     ULong   DateInSec;
>     CharPtr  theConvertDate;
>
>     // initilize the varibles:
>     the_date->month = 12;
>     the_date->day = 8;
>     the_date->year = 79;
>
>     theConvertDate = "12/08/79";
>
>     // get the seconds since 1/1/1904
>     DateInSec = TimGetSeconds ();
>
>     // convert the secounds to the current date
>     DateSecondsToDate ( DateInSec, the_date );
>
>     // convert the date to a string
>     DateToAscii ( the_date->month, the_date->day, the_date->year,
>dfDMYWithDots,
>     theConvertDate );
>
>     return theConvertDate;
>
>}
>
>the Qs are:
>1.) do i need to add teminate to the "theConvertDate" ( to add /0) or the
>DateToAscii does it??

>      and if i need to add it myself how do i do it? how can i add it at
the
>end??
>2.) i would like to get suggestions from others that have a deffirente kind
>of view or same code.
>
>if u can send me the answer to my own email.
>
>tnx in advance :-)
>
>
>
>



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

Reply via email to