Thread: concat strings

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    83

    concat strings

    Hi,
    I'm trying to write a function that concats a string onto the end of the previous.
    So, if I first pass it ABC
    the buffer string becomes "ABC"
    then I pass it DEF
    and the buffer string becomes "ABCDEF"

    Any suggestions?

    Thanks

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Start writing the code and post it here if and when you have any questions or issues.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll need a static thing that keeps track of the string-so-far. Are you allowed to use what's in string.h? Otherwise you'll have to rewrite strcat here too.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    I can use string.h
    strcat is good...

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    but how do i dynamically allocate for this?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by pollypocket4eva View Post
    but how do i dynamically allocate for this?
    Do you need to dynamically allocate? Is char the_answer[256] (or whatever) not going to work?

    If you need to dynamically allocate, then you need two static things -- a pointer and a size, and you would use realloc to add the new size to the old size and go from there.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    I'm getting the warning: "passing argument 2 of 'strcat' from incompatible pointer type"

    I'm getting my element from a file...
    fread(&element, 3, 1, input);

    and concatting with:
    strcat (buffer, &element);

    Is this wrong?

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    83
    please help

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by pollypocket4eva View Post
    I'm getting the warning: "passing argument 2 of 'strcat' from incompatible pointer type"

    I'm getting my element from a file...
    fread(&element, 3, 1, input);

    and concatting with:
    strcat (buffer, &element);

    Is this wrong?
    Apparently. If element is a char array or a char *, then the &'s are incorrect.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Can you post more of the code snippet you are using for the string catenation.
    Last edited by itCbitC; 11-10-2008 at 02:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM