Hi ,
char date[2] = "02";
char *temp_date;
how can i copy date to temp_date.
I tried strcpy and it gives a memory exception violation;
Please advice,
thanks
Hi ,
char date[2] = "02";
char *temp_date;
how can i copy date to temp_date.
I tried strcpy and it gives a memory exception violation;
Please advice,
thanks
That depends on what you mean by "copy date to temp_date". If you just want temp_date to point to the date you've got, then do so, with "temp_date = date". If you want a separate copy, then you will first need to acquire enough memory for the separate copy to live in (using malloc and friends).
Not only that, but "02" takes up 3 bytes (don't forget the null terminator), yet you have only allocated 2 for your date array.
bit∙hub [bit-huhb] n. A source and destination for information.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Possibly, except that it is non-standard.Originally Posted by Kennedy
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Really? According to who's standards?
The 1999 edition of the C Standard, and without checking I daresay the previous edition as well.Originally Posted by Kennedy
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Who cares how standard it is when implementing it yourself is like, a two line function?
Code:char *MyStrdup( const char *str ) { char *dup = malloc( strlen( str ) + 1 ); return dup ? strcpy( dup, str ) : dup; }
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}