Thread: Concatenating strings

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    14

    Concatenating strings

    I wanted to do a program with concatenating strings 'strcat()', where this function accepts two strings, but the second must be a constant string. This is a problem, because if I need more than 1 concatenating, then I need more than 1 constant strings, instead of using the same every time.

    I was just wondering why is it necessary that the second string is constant? I know it's because C language is just made like that, but I was wondering why.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    When there is the "constant" keyword in front of a parameter it means that the parameter doesn't change value inside the function. Which can be true foa strcat() function.
    What is the benefit? The compiler can optimize by replacing directly the value. For example
    Code:
    char* foo(const char* my)
    {
       return my;
    }
    
    char* boo(char* my)
    {
       my = "hey";
       return my;
    }
    When calling boo the parameter will be copied in a temporarily variable, my. My changes its value, there is no other way (it bocomes "hey").
    When calling foo, my doesn't change. So the compiler can optimize and not create a temporarily varialbe my. Just replace my with the value inserted.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Not sure how much it has to do with optimization. It just might interest the caller whether a function will modify its pointer arguments or not.

    That const tells you whether you can pass a string literal (which are unmodifiable) as the second argument. Unfortunately, C allows implicit conversion of const char* to char*, so you could also pass a string literal to an argument that is going to be modified in the function (and probably crash the program doing so), but at least compilers emit warnings if you do that (at least GCC compiling as C++ does).

    Code:
    #include <ctype.h>
    
    void foo(char* str)
    {
        str[0] = toupper(str[0]);
    }
    
    void bar(const char* str)
    {
        //str[0] = toupper(str[0]);
    }
    
    int main()
    {
        foo("hello"); //wrong: function is going to attempt to modify a literal
        bar("hello"); //safe: function won't modify argument
        return 0;
    }
    What is the benefit? The compiler can optimize by replacing directly the value. For example
    That doesn't compile (you are trying to return a non-constant pointer from a constant pointer).

    And even then, what is constant is the string pointed to by my. What is not constant is the copy of the pointer itself:

    Code:
    const char* foo(const char* my)
    {
        my = "hey"; //my now points to string literal "hey"
        return my;
    }
    Last edited by anon; 11-29-2009 at 10:27 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Easier way of concatenating a lot of strings?
    By Evenstevens in forum C Programming
    Replies: 2
    Last Post: 05-09-2009, 01:29 PM
  2. concatenating strings?
    By liri in forum C Programming
    Replies: 11
    Last Post: 08-24-2007, 05:34 PM
  3. Concatenating 2 strings
    By AngKar in forum C Programming
    Replies: 14
    Last Post: 04-25-2006, 10:51 AM
  4. concatenating more than 2 strings
    By Grunt12 in forum C Programming
    Replies: 3
    Last Post: 11-25-2005, 05:31 PM
  5. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM