Help with strings and functions.

This is a discussion on Help with strings and functions. within the C++ Programming forums, part of the General Programming Boards category; Hi there. i made this function: Code: char* strmerge(const char str1[], char str2[]) { int x=0, chars=0; char str_merged[40]; while ...

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

    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
    21,457
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    84
    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
    21,457
    Yes. The array in that location should also be modifiable, e.g., it should not be a string literal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    84
    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
    21,457
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    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, 12:38 AM
  4. Creating Functions that use Format control Strings
    By tzuchan in forum C Programming
    Replies: 6
    Last Post: 03-29-2004, 11:50 AM
  5. Using Strings with Functions
    By DJH in forum C Programming
    Replies: 10
    Last Post: 10-19-2001, 04:40 PM

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