Copy char array to char pointer

This is a discussion on Copy char array to char pointer within the C Programming forums, part of the General Programming Boards category; Hi , char date[2] = "02"; char *temp_date; how can i copy date to temp_date. I tried strcpy and it ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    Copy char array to char pointer

    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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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).

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    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 sure how many friends malloc() would need here. Anyway, another option:
    Code:
    char date[3] = "02";
    char temp_date[3];
    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

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by MK27 View Post
    Not sure how many friends malloc() would need here. Anyway, another option:
    Code:
    char date[3] = "02";
    char temp_date[3];
    strdup() anyone?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by MK27 View Post
    Not sure how many friends malloc() would need here.
    In this case, just one (free).

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Kennedy
    strdup() anyone?
    Possibly, except that it is non-standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by laserlight View Post
    Possibly, except that it is non-standard.
    Really? According to who's standards?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by Kennedy
    According to who's standards?
    The 1999 edition of the C Standard, and without checking I daresay the previous edition as well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ANSI C parsing char array as pointer
    By cyberjunky in forum C Programming
    Replies: 3
    Last Post: 11-30-2008, 02:25 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21