Thread: strcopy for an array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    no the way u've done it, it should be newstring[i] = '\0';

    but personally this is the way i would do it.

    since '\0' character value is 0, the loop will end with that value.

    Code:
    for(i = 0; array[i]; ++i)
        newstring[i] = array[i]
    
    newstring[i] = '\0';
    actually i missed something in ur code above, it shouldn't be

    Code:
    char *newstring = new char[strlen(array)];
    it should be

    Code:
    char *newstring = new char[strlen(array)+1];
    requires the +1 for the '\0' terminator
    Last edited by Kyro; 11-04-2003 at 08:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM