Thread: char* char || HELP!!! please

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    England, Norwich
    Posts
    38

    Question char* char || HELP!!! please

    Hey


    i was wondering if theres a way to add a single character to a char* Variable.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There are many ways.

    1. Make a short string from your char, and call strcat.
    char s[2]; s[0]=ch; s[1]='\0';
    kind of thing

    2. find the end (strchr or strlen) and append the char yourself, but don't forget to also append a \0

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    England, Norwich
    Posts
    38
    Thanks For The replily but it didnt realy help me the ponit of understanding how i would do it

    say i have

    char* bla;
    and
    char bla2;

    and i want to add chr bla2 onto the end of bla

    any ideas?

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    Assuming you know that the size of blah2 is small enough to fit within bla, then you could use the strcat function.

    Code:
    strcat(bla, blah2);

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318

    Post

    This will do what you want:
    Code:
    char *temp = &bla[strlen(bla)];
    *temp++ = bla2;
    *temp = '\0';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-22-2008, 07:20 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM