Thread: Adding to char variables together

  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
    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, 09: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, 09: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