Concatenation ?

This is a discussion on Concatenation ? within the C Programming forums, part of the General Programming Boards category; my objective is to strcat(src1, src2) and save it to the variable result. My attempt: result = strcat(src1, src2); But ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    14

    Concatenation ?

    my objective is to strcat(src1, src2) and save it to the variable result.



    My attempt:


    result = strcat(src1, src2);


    But some people are saying my theory is wrong. Can someone show me how to save my result from strcat(src1, src2) to the variable result please ?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Your theory is wrong. strcat() always returns the dest pointer anyway, so capturing it is usually pointless. Read the man page, carefully:
    Code:
    SYNOPSIS
           #include <string.h>
    
           char *strcat(char *dest, const char *src);
    
           char *strncat(char *dest, const char *src, size_t n);
    
    DESCRIPTION
           The  strcat()  function appends the src string to the dest
           string overwriting the `\0' character at the end of  dest,
           and  then  adds a terminating `\0' character.  The strings
           may not overlap, and the  dest  string  must  have  enough
           space for the result.
    
           The  strncat()  function  is similar, except that only the
           first n characters of src are appended to dest.
    
    RETURN VALUE
           The strcat() and strncat() functions return a  pointer  to
           the resulting string dest.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    14
    Sorry for being a noob. My second attempt. Please help if u see something wrong:

    src1 = strcat(src1, src2)

    result = strcpy(src1) // result = src1 + src2 ?

    Would that work?

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    strcat(param1 , param2);

    strcat will allways concat param2 to param1 ! Depending on what you are trying to do you can also use sprintf.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    main() {
            char src1[]="Hello ";
            char src2[]="World !";
            char buff1[BUFSIZ];
    
            printf("src1: [%s]\n",src1);
            printf("src2: [%s]\n",src2);
            sprintf(buff1,"%s%s",src1,src2);
            printf("buff1: [%s]\n",buff1);
            strcat(src1, src2);
            printf("src1: [%s]\n",src1);
            printf("src2: [%s]\n",src2);
    }
    Output:
    src1: [Hello ]
    src2: [World !]
    buff1: [Hello World !]
    src1: [Hello World !]
    src2: [World !]

    Hope it helped !

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    result = strcpy(src1) // result = src1 + src2 ?
    You should use strcpy() like this:

    strcpy (result, src1);

    The function returns result.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    14
    src1 = strcat(src1, src2)

    result = strcpy(result, src1)


    Thank you guys

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    BTW, here's a useful online reference.
    http://www.cppreference.com/

  8. #8
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    Quote Originally Posted by koolguysj
    src1 = strcat(src1, src2)

    result = strcpy(result, src1)
    Theres no need to capture the result of strcat or strcpy, in this situation.

    this should suffice if you really want to use an extra variable:
    Code:
    ...
    strcpy(result,src1);
    strcat(result,src2);
    ...
    No need to assign the result of strcpy or strcat to anything.

  9. #9
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    520
    Quote Originally Posted by groorj
    strcat(param1 , param2);

    Code:
    ...
            char src1[]="Hello ";
            char src2[]="World !";
    ...
            strcat(src1, src2);
    ...
            return 0;
    }
    This is will most likely cause a seg fault. You're writing past the end of your array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 10:31 AM
  2. printing arrays with concatenation
    By derek23 in forum C Programming
    Replies: 1
    Last Post: 07-17-2005, 04:02 AM
  3. concatenation
    By F*SH in forum C++ Programming
    Replies: 34
    Last Post: 11-13-2002, 06:47 PM
  4. queue concatenation
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2002, 06:35 AM
  5. integer concatenation with string again...
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:36 PM

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