Thread: beginner question: returning a string from a function

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    1

    beginner question: returning a string from a function

    I've been looking at how to achieve this, and I see that it's not possible using the approach that I first assumed would work (return "the string"

    For example, I need the first line here:
    Code:
    char *getString(void);
    main(){
        char *test = getString();
        printf("%s\n\n", test);
    }
    
    char *getString(){
        char *test = (char *)malloc(20*sizeof(char));
        test = "this is a string";
    
        return test;
    }
    I've tried looking for an explanation on why this is needed, what it does etc but I still cant understand what's happening.

    Could someone explain what the above code is doing?
    thanks for reading!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This code "works", but not because it's correct, just because it "happens to".
    Multiple issues here. First, string literals, such as "hello world", are really pointers. const char* to be exact. So first you malloc memory, then you overwrite that pointer with something else. Memory leak.
    But since string literals are stored in a part of your executable, you can return them as you wish. They work just fine.

    Otherwise, you need to send in a buffer to your function, including the size of your buffer, and copy data into it. Make sure you don't copy more data than you have room. That's why you need your size parameter.
    And finally, main returns int and your getString prototype is mismatching.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    In other words, instead of
    Code:
    test = "this is a string";
    you should have
    Code:
    strcpy(test, "this is a string");

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't forget that test must an array with sufficient storage, such as:
    Code:
    char test[100];
    strcpy(test, "This is a string");
    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.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Well, he's allocating 20 bytes which is sufficient for his example.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    nonoob's suggestion is a correct one and Elysia's assertion is certainly right, but if you integrate Elysia's example into your code, you would then be returning a pointer to a local variable, which is wrong. Rather, one might write:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *getString(void);
    
    int main(void) {
        char *test = getString();
        printf("%s\n", test);
        free(test);
        return 0;
    }
    
    char *getString(void) {
        char *test = malloc(20 * sizeof(*test));
        strcpy(test, "this is a string");
        return test;
    }
    Observe that the correct headers were included, and that I removed the cast of the return value of malloc. Furthermore, sizeof(char) is always 1, so you could leave it out, but I chose to change it to the more general sizeof(*test).

    EDIT:
    Additionally, one should check that malloc did not return a null pointer before accessing the memory presumed to be allocated.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This amounts to having to free the malloc later which is certainly easy to forget and error prone. Furthermore, it is inflexible, because you cannot choose whether to use dynamic memory or not. And it is slower because dynamic memory is slower than stack based allocation.
    Which amounts to passing in a buffer to the function as I mentioned in the first place.
    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. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM