Thread: Using strcpy?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Using strcpy?

    I have 2 strings.
    char* a = "hi";
    char* b = "";

    How can I use strcpy to copy string a to string b?
    please provide me the codes and give me explanation if possible.
    Thank you very much.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Phantom2303 View Post
    I have 2 strings.
    Nope. In C, you have two pointers and one memory overflow.

    Two strings would be like this:
    Code:
    char a[]="hi";
    char b[309];
    
    strcpy(b,a);
    
    printf("%s\n%s\n");
    Output:
    hi
    hi
    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

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    printf("%s\n%s\n"); - something is missing. Two somethings...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Perhaps better questions are;

    1. Why didn't you use strncpy()
    2. Where does 309 come from?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Use strncpy.

    For copying the string from source to destination.
    always better use strncpy --> so that overflow can be avoided.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by shwetha_siddu View Post
    For copying the string from source to destination.
    always better use strncpy --> so that overflow can be avoided.
    strncpy could leave the string without nul-terminator
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Where is strcpy() defined? (Can't find in string.h ect)
    By Zero_Point in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 05:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM