Thread: Copy char array to char pointer

  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,336
    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,268
    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,336
    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
    28,413
    Quote Originally Posted by Kennedy
    strdup() anyone?
    Possibly, except that it is non-standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 03:25 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09: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, 02:19 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM