concat strings

This is a discussion on concat strings within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to write a function that concats a string onto the end of the previous. So, if I ...

  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
    Katy, Texas
    Posts
    2,309
    Start writing the code and post it here if and when you have any questions or issues.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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,185
    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,185
    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,058
    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, 07:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

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