Adding to char variables together

This is a discussion on Adding to char variables together within the C++ Programming forums, part of the General Programming Boards category; For example if I have one char variable "coo" and the other "kie", how can I put them together to ...

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Adding to char variables together

    For example if I have one char variable "coo" and the other "kie", how can I put them together to one variable without making them to strings?

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    well, "coo" isn't a char variable, it's a char array, which is a string. So if what you mean is that you don't want to put them into an std::string object, then you can do something like this:

    char *c = "coo";
    char *k = "kie";

    char *ck = new char[strlen(c) + strlen(k) + 1];
    sprintf(ck, "%s%s", c, k);

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I ment more like this
    char first[500]="coo";
    char second[500]="kie";
    char thisshouldbewhereiwanttoaddthemtogether[500];

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    maybe this way
    Code:
    strcpy( thisshouldbewhereiwanttoaddthemtogether, first);
    strcat( thisshouldbewhereiwanttoaddthemtogether, second);
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM

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