Thread: how to i made a full copy of a char pointer?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    9

    how to i made a full copy of a char pointer?

    Hi,

    I am a newbie.

    i would like to copy a string1 = " hello world "
    to
    char* buffer.

    This buffer is part of a class that i have created. the string1 is a parameter that will be sent into my constructor as a parameter.

    Hope i paint out my problem clearly.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Are you talking about the string class or an array of chars?

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    Class::Class(char *src)
    {
      char* buffer new[strlen(src)+1];
      strcpy(buffer, src);
    }
    There are other ways of course. *cues the host of other posts showing other methods*

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    If the char array is null terminated, then the strcpy method will work. If you have a string that is not null-terminated(for instance like the char array returned for a REG_MULTI_SZ from the registry), you can use strncpy(dest, source, size). This will allow you to copy data that exists beyond the null terminator.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    i would like to copy a string1 = " hello world "
    to
    char* buffer.

    Hope i paint out my problem clearly.
    Nope. You didn't say what type string1 is.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    /me adds to thantos' program

    Code:
    Class::Class(char *src)
    {
      char* buffer new[strlen(src)+1];
      strcpy(buffer, src);
    }
    Class::Class(std::string src)
    {
      char* buffer new[src.length()+1];
      strcpy(buffer, src.c_str());
    }
    edit, yeah, I'm not actually sure if that works (no access to a compiler)... but the logic's there
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    Thantos has solved my problem. Thanks a lot!
    and thanks to everyone

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by mercury529
    If the char array is null terminated, then the strcpy method will work. If you have a string that is not null-terminated(for instance like the char array returned for a REG_MULTI_SZ from the registry), you can use strncpy(dest, source, size). This will allow you to copy data that exists beyond the null terminator.
    Well if the only parameter is the pointer you have to assume that it uses some character to mark the end. If they had said the length was passed also I would have used strncpy.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    Quote Originally Posted by Thantos
    Well if the only parameter is the pointer you have to assume that it uses some character to mark the end. If they had said the length was passed also I would have used strncpy.
    Oh just giving them additional information for if they ever deal with non-null terminated char arrays which occasionally come into play.

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    I'm not sure if it's strictly ANSI, but at least on MS compilers there is a standard function called _strdup which accepts a char pointer and returns a new pointer to a new chuck of memory containing a copy of your original string. Only works with regular zero terminated character arrays.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy character array to char * pointer
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 02-20-2009, 12:33 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM