Thread: Function - allocate new string and copy X characters

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    4

    Exclamation Function - allocate new string and copy X characters

    I'm supposed to write a function that receives a string and a number X as arguments, allocates a 2nd string of minimum length and copies first X characters of 1st string to the 2nd string and returns the 2nd string.

    What would be the correct code for that?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Some combination of malloc() and strncpy().

    Why don't you try it for yourself first?
    We don't typically hand out code without you making an effort first.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-20-2011, 11:56 AM
  2. Replies: 4
    Last Post: 06-01-2010, 08:28 PM
  3. pre-allocate memory for string
    By TheBigOnion in forum C++ Programming
    Replies: 6
    Last Post: 03-30-2010, 01:10 PM
  4. string copy function
    By rahulsk1947 in forum C Programming
    Replies: 8
    Last Post: 02-14-2006, 07:13 PM
  5. function to copy one string to another one
    By robstr12 in forum C Programming
    Replies: 15
    Last Post: 01-28-2005, 11:46 PM