Thread: String woes

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

    String woes

    I've been playing around a bit and attempting to create a char method which accepts strings (char arrays), does something with them, and then returns a string. However, I'm running into more trouble than I seem to be able to figure my way out of. And most internet articles are of little help, because most of the code examples I find produce the same warnings that I get, or even other ones.

    Code:
    char textadd(char *, char *);   // string method
    
    main() {
            char str1[10];
            char str2[10];
            strcpy(str1, "Sometext");
            strcpy(str2, "Moretext");
            textadd(str1, str2); <----------- send the to char arrays/strings to the textadd() method
    OR        
            printf("%c\n", textadd(str1, str2));
     }
    
    char textadd(char *s1, char *s2) {
            int newsize = (strlen(s1)+strlen(s2)+1);  // - the size the of the two strings combined. 
            char ns1[newsize];                                     // - Create a new string with the new size.
            strcpy(ns1, s1);                           // - Copy the contents of s1 into the new string.
            strcat(ns1, s2);                            // - Run strcat() on the new string (which contains s1) and s2.
            printf("New string: %s\n", ns1);   <--- PRINTS OUT THE COMBINED STRINGS FINE
            printf("%p\n", &ns1);                     <--- PRINTS OUT THE ADDRESS FINE
    
            // Then I try to return the string:
            return ns1;     
    }
    When I try returning the string as above, I get the following warning:

    methods.c:71: warning: return makes integer from pointer without a cast
    methods.c:71: warning: function returns address of local variable


    So, I cannot return a char array from a char method?

    And - I cannot return the address, because the variable contaning the new string is local.

    If I do
    Code:
    return *ns1;
    instead, the returned value is an integer (83). Not very helpful, though I am curious as to where it comes from.

    So basically what I'm wondering about is how to do this when involving arrays. With int or double variables it's easy, but I need a lot of work on getting used to the way strings/arrays are handled in C.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cnewbie1 View Post
    methods.c:71: warning: return makes integer from pointer without a cast
    methods.c:71: warning: function returns address of local variable
    The first one is because the type of the function is "char" (single chars are integer values), but you are returning a "char*" -- a char pointer. So make it:
    Code:
    char *textadd(char *s1, char *s2)
    The second one is because ns1 is a local stack variable. Every function, when called, gets a "stack" of memory allocated to it. This contains all of it's internal variables. When the function is over, that stack is freed.

    If you want to return a pointer, you must allocate it memory on the global heap using malloc:
    Code:
    char *ns1 = malloc(newsize);
    There is only one heap, and nothing in it is freed unless you make it so. Which it is important to understand that you should, when you are done using such a heap variable, by using free().

    However, it's hard for me to give an example of how to use free since you do not actually use the return value of the function! A return value, to be useful, must be assigned:

    Code:
    char *str3;
    str3 = textadd(str1, str2);
    [...when done w/ str3:]
    free(str3);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Slicing problem with inherited classes.
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 10-20-2009, 02:29 PM
  2. student looking for some help.
    By akairyuu in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2009, 02:26 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM