Thread: strcat

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    3
    Rep Power
    0

    strcat


    Hi

    I have hit upon a problem in the program I am currently writing in C. I have a string
    Code:
    char *text;
    and I have searched through this string, incrementing the pointer with
    Code:
    text++;
    until I get to the character I want to copy. Then I want to append this character to another string:
    Code:
    char *temp;
    which I initialise to empty string:
    Code:
    temp = "";
    Now I want to add the current 'text' character to the 'temp', then move the pointer along the text and append the next charcter, (until some test stops the loop).
    Code:
    while (*text != 'X') {
    
    strcat(test,*text);
    text++
    }
    I keep getting conversion errors because the parameters of strcat are wrong but I can't work out what is wrong. Can anyone help?


    I just want to append the first character pointed to by 'test', to 'text'. It seems so simple but I can't get it to work!!!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,143
    Rep Power
    2222
    strcat expects the second parameter to be a string (i.e., a char pointer). You cannot use it to append a single character as a char.

    I would think either create a string out of the character (copy it to the first element of a two-char array whose second char is '\0' and apply that array to strcat). Or simply set the destination string's null-terminator position to the new character and the next position to '\0'.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    3
    Rep Power
    0
    Can you show me how to create an empty string and with the character pointed to by *text and the null character. And then how to use it with strcat?

    Thanks
  6. #4
  7. Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jan 2004
    Location
    near St. Louis Illinois
    Posts
    3,288
    Rep Power
    24
    The initial allocation of your variable text has to be large enough to hold all the characters that it is to contain. In your example, it is only large enough for one character -- the null terminator. you need to do something like this:
    Code:
    char *temp = malloc(80); // hold up to 79 characters
    // make it an empty string
    temp[0] = 0; 
    // add something to it
    strcpy(temp,"Some stuff ");
    // contantinate a string to the end
    strcat(temp,"Hello World");
    or you could also do it this way
    Code:
    char temp[80]; // hold up to 79 characters
    // make it an empty string
    temp[0] = 0; 
    // add something to it
    strcpy(temp,"Some stuff ");
    // contantinate a string to the end
    strcat(temp,"Hello World");
    Last edited by Ancient Dragon; February 2nd, 2004 at 06:25 AM.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    3
    Rep Power
    0
    that worked great thanks, had me stumped for ages, not used to this manual memory allocation business!!

    by the way, needed to cast the malloc before using it...

    Code:
    char * temp = ( char * ) malloc ( 80 );

IMN logo majestic logo threadwatch logo seochat tools logo