Like Tree1Likes
  • 1 Post By Salem

Function - allocate new string and copy X characters

This is a discussion on Function - allocate new string and copy X characters within the C Programming forums, part of the General Programming Boards category; I'm supposed to write a function that receives a string and a number X as arguments, allocates a 2nd string ...

  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
    32,834
    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.
    smokeyangel likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 09:28 PM
  3. pre-allocate memory for string
    By TheBigOnion in forum C++ Programming
    Replies: 6
    Last Post: 03-30-2010, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21