Thread: copy = concatenate ?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Question copy = concatenate ?

    As a newbye, I've justed started writing some code.
    Instead of writing 2 different functions for COPY and CONCATENATE string, I'm trying one that will do both:
    a) if destination is empty then it will be a simple copy from original string to destination,
    b) if destination is not empty, then a concatenation applies.

    So, my code is as follows:

    #include <stdio.h>


    // a CONCATENATE function is similar to a COPY function, with the only
    // difference that the copy to destination starts on first empty index

    Code:
    char *f_strcpy(char *v_string)
    
    {
    
      int length = 0; // determines what is actual length of destination string
      int i;
      char dest[100];
    
      // check for length, ignoring termination character
      for(i=0; dest[i]!='\0'; i++)
           ;
    
      length = i; // 
    
      // starts concatenation (or simple copy if destination is empty)
      {
        for(i=0; v_string[i]!='\0'; i++)
          dest[length+i]=v_string[i];  // leaves cycle as soon as ='\0'
      }
    
      dest[length+i] = '\0';     // assigns terminator after leaving cycle - otherwise
    
      return dest;
    }
    Now, if i run this code, it returns some strange characters.

    Any ideas?
    cheers,
    Arr

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    You are returning a pointer to variable (dest) that is local to your function. If you insist on using a fixed length character array, make it static, otherwise use malloc to create a dynamically allocated character array which you can safely return.

    You are also scanning though dest when it isn't initialised.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. dest is not initialized - undefined behavior of the loop checking for the length
    2. returning pointer to the local variable - memory is destroyed on function exit, pointer is dan
    gling
    3. You don't check the destination buffer length (possible memory overrun).
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are using dest uninitialized. Aren't you supposed to be counting v_string's length instead?
    You can't return local variables. You'll need to use something like malloc.

    [edit]
    Curses, foiled again!
    [/edit]

    [edit2]
    Your problem is the concept of your function, really. What exactly are you trying to copy / fill? If "it's" empty... What's "it"? You imply that you have some kind of pointer that you need to be adding to. If you already have a string you want to add to, why aren't you passing both the one you want to add to and the one you are adding from?

    You really need two arguments. If the first is NULL, duplicate the second. If it's not, then stick the two together.
    [/edit2]

    Quzah.
    Last edited by quzah; 11-03-2006 at 05:04 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  2. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  3. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  4. Copy Constructor crashing program
    By bob2509 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 04:21 PM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM