Thread: Passing string to function

  1. #1
    Registered User PotitKing's Avatar
    Join Date
    Dec 2001
    Posts
    28

    Passing string to function

    I've written this code for an exercise i found in a tutorial (without answer ). When i run it the szNew string doesen't have a value at all. I'm not very good with a debugger, so i wondered if you guys could help me out.

    Compiler: gcc-2.95.3
    Os: Debian 3.0 woody (Linux kernel 2.4.18)

    Here's the code:

    #include <stdio.h>
    #include <stdlib.h> /* For malloc() */
    #include <string.h> /* For strlen() */

    char* concat_strings(char* szTarget, char* szSource, char *p);

    int main()
    {
    char szString_1[100] = "hello "; /* Just a test value */
    char szString_2[100] = "maan!";
    char *szNew;
    szNew = (char*) malloc(strlen(szString_1) && strlen(szString_2)); /* Allocating memory for the string (it should be the size of szString_1 and szString_2 */
    szNew = concat_strings(szString_1, szString_2, szNew);
    puts (szNew);
    return 0;
    }

    char* concat_strings(char* szTarget, char* szSource, char *p)
    {
    puts(szTarget); /* Just to view the values */
    puts(szSource);
    while((*p++ = *szTarget++) != 0) /* Adding the first string to a new string */
    {
    }
    while((*p++ = *szSource++) != 0) /* Adding the second string to the new string. */
    {
    }
    return p;
    }
    Last edited by PotitKing; 05-24-2002 at 01:18 PM.
    % gcc -v
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 3.3.3 [FreeBSD] 20031106

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    What exactly do you want?
    I compiled the program fine and I get this as output:
    Code:
    hello 
    maan!
    The world is waiting. I must leave you now.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Some hints:

    1.
    You want szNew to have such a length that both szString_1 and szString_2 can be stored in it.

    szNew = malloc (strlen(szString_1) + strlen(szString_2));

    Note that you used &&, which would result in 1 because both strings have length > 0.

    2.
    >while((*p++ = *szTarget++) != 0)

    In the function you are increasing the pointer, which means that at the end p points to the end of the string.

    Hope these two hints helped you a little further.

  4. #4
    Registered User PotitKing's Avatar
    Join Date
    Dec 2001
    Posts
    28

    isn't it obvious?

    I want to concatenate the strings (i know about strcat, but I want to write the function for the learning of doing it)
    % gcc -v
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 3.3.3 [FreeBSD] 20031106

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Shiro has pointed out half the problem here
    > szNew = (char*) malloc(strlen(szString_1) && strlen(szString_2));
    1. it should be +, not &&
    2. you need a +1 as well, to allow for the \0
    3. the cast isn't necessary (perhaps even harmful) in ANSI-C
    So
    szNew = malloc( strlen(szString_1) + strlen(szString_2) +1 );

    There are also 2 problems with your function
    1. There is no \0 added to the string
    After both while loops, add
    *p = '\0';

    2. return p;
    But you modify p when you run the while loops, so when you return....
    szNew = concat_strings(szString_1, szString_2, szNew);
    You also modify szNew in the caller function (this is bad)

    Personally, returning nothing (void) would be better in this case
    void concat_strings(char* szTarget, char* szSource, char *p);
    You're not going to return anything you don't know already.

  6. #6
    Registered User PotitKing's Avatar
    Join Date
    Dec 2001
    Posts
    28

    What was I thinking?

    I knew that not returning the value was possible, but the exercise told me not to (I think), so I made this alternative code thanks to you guys

    #include <stdio.h>
    #include <stdlib.h> /* For malloc() */
    #include <string.h> /* For strlen() */

    void concat_strings(char* szTarget, char* szSource, char *p);

    int main()
    {
    char szString_1[100] = "hello "; /* Just a test value */
    char szString_2[100] = "maan!";
    char *szNew;
    szNew = szNew = malloc( strlen(szString_1) + strlen(szString_2) +1 ); /* Allocating memory for the string (it should be the size of szString_1 and szString_2 */
    concat_strings(szString_1, szString_2, szNew);
    puts (szNew);
    return 0;
    }

    void concat_strings(char* szTarget, char* szSource, char *p)
    {
    strcpy (p, szTarget);
    while(*p != 0)
    {
    p++;
    }
    while((*p++ = *szSource++) != 0) /* Adding the second string to the new string. */
    {
    }
    *p = '\0';
    }


    This works (I had to replace the first while loop with strcpy to make it work, don't know why, so I just dit ). Thanks a lot. This really helped me understand strings a lot more.
    % gcc -v
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 3.3.3 [FreeBSD] 20031106

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You probably forgot to remove the copied '\0'.

    Code:
    void concat_strings (char* szTarget, char* szSource, char *p) 
    {
        char *temp;
        temp = p;
    
        while ((*temp++ = *szTarget++) != '\0');
    
        /* Remove '\0' */
        temp--;
        
        while ((*temp++ = *szSource++) != '\0');
    
        *temp = '\0';
    }

  8. #8
    Registered User PotitKing's Avatar
    Join Date
    Dec 2001
    Posts
    28

    thanks

    Thanks for the tip. I noticed you used another pointer, temp. Why? Isn't it good enought just using the p pointer?
    % gcc -v
    Configured with: FreeBSD/i386 system compiler
    Thread model: posix
    gcc version 3.3.3 [FreeBSD] 20031106

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: thanks

    Originally posted by PotitKing
    Thanks for the tip. I noticed you used another pointer, temp. Why? Isn't it good enought just using the p pointer?
    Because by incrementing it, you're changing what it points to. As such, if you return it's new location, you're no longer pointing at the beginning of the string. By using a temporary pointer, you preserve the pointer to the beginning of the string.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing string to function
    By R.Stiltskin in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2009, 12:56 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM