Thread: Help with strings and functions.

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    120

    Help with strings and functions.

    Hi there.

    i made this function:
    Code:
    char* strmerge(const char str1[], char str2[])
    {
        int x=0, chars=0;
        char str_merged[40];
    
        while (str1[x]!='\0')
        {
            str_merged[chars]=str1[x];
            x++;
            chars++;
        }
    
        x=0;
    
        while (str2[x]!='\0')
        {
            str_merged[chars]=str2[x];
            x++;
            chars++;
        }
        str_merged[chars]='\0';
    
        return str_merged;
    }
    this function takes 2 strings and puts them together, but there is a problem, when i call the function like this for example:
    Code:
    strmerge ("Hello", "testing");
    this should return Hellotesting, but instead it only "Hell" and then some random chars, for example:

    "Hell└@G"

    why does this happen and how can i solve it??

    help pls

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One problem is that you are returning a pointer to a local variable that no longer exists once control returns from the function.

    One option is to provide yet another parameter for the destination array, or you could do what strcat does and make one of the two input arrays the destination array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by laserlight View Post
    One option is to provide yet another parameter for the destination array,
    tthis means a pointer to an outside string location right??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. The array in that location should also be modifiable, e.g., it should not be a string literal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    120
    Ok tks for the help, i did it.

    here it is for the curious ppl:

    Code:
    int strmerge(const char str1[], char str2[], char *p)
    {
        int x=0;
    
        while (str1[x]!='\0')
        {
            *p=str1[x];
            x++;
            p++;
        }
    
        x=0;
    
        while (str2[x]!='\0')
        {
            *p=str2[x];
            x++;
            p++;
        }
        *p='\0';
    }
    and i learned alot about pointers today as well :P
    Last edited by shiroaisu; 08-03-2010 at 01:30 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Note that you can change the two instances of *p++; to p++; since there is no point dereferencing p at that point. You might want to get rid of the chars variable since it is not used.

    Also, if you are always going to return 1, then it might make sense to return nothing at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    120
    Ok tks for the info, i edited my previous post with the changes

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Always when dealing with external buffers (buffers not created in your function), you MUST, and I repeat, you MUST, pass in the buffer size.
    Then USE this parameter to make sure you don't write off the end of the array.
    Very very important.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions for C strings
    By officedog in forum C Programming
    Replies: 34
    Last Post: 09-25-2009, 04:29 PM
  2. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  3. passing strings from functions as the "return" part
    By fraktal in forum C Programming
    Replies: 8
    Last Post: 12-13-2005, 01:38 AM
  4. Creating Functions that use Format control Strings
    By tzuchan in forum C Programming
    Replies: 6
    Last Post: 03-29-2004, 12:50 PM
  5. Using Strings with Functions
    By DJH in forum C Programming
    Replies: 10
    Last Post: 10-19-2001, 04:40 PM