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
Printable View
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.
Possibly, except that it is non-standard.Quote:
Originally Posted by Kennedy
Really? According to who's standards?
The 1999 edition of the C Standard, and without checking I daresay the previous edition as well.Quote:
Originally Posted by Kennedy
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;
}