return values of functions

This is a discussion on return values of functions within the C Programming forums, part of the General Programming Boards category; In this function I'm trying to return the array fn so other parts of the program can use it Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    return values of functions

    In this function I'm trying to return the array fn so other parts of the program can use it

    Code:
    char new_book()
    {
    char fn[104];
        printf("What do you want your new address book to be called: ");
        scanf("%s", &fn);
        strcat(fn, ".dg");
        return fn;
        
    }
    but it doesn't complie with my DJGPP complier. So can someone please help me.

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Well there are a couple of things wrong with what you're trying to do.

    First, you've declared your function return type as type char. However, you're trying to return the array. These are incompatible types.

    Second, you're really returning only the address of your array. That's how you return arrays anyways. However, your array is temporary and your array will be out of scope when you return. This will make the address returned invalid and this is dangerous because you may modify addresses you're not supposed to.

    Thirdly, use fgets instead of scanf. This is safer because you can check the length.

    A solution would be to declare your function return type as:
    char * new_book()

    Dynamically allocate some memory for your string and return it after you have filled it up with the data you want. You can always use a global char array to store the value though, but what would be the fun of that
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    This won't work, you can't return an array in C. You can fake it by returning a char *, but in this case it still won't work because when the function returns, the memory for fn is freed and the pointer that you pass back will point to garbage. The best way to do this is to have the calling function allocate an array and pass it to your function.
    Code:
    char *new_book( char fn[] )
    {
            printf( "What do you want your new address book to be called: " );
            scanf( "%s", &fn );
            strcat( fn, ".dg" );
    
            return fn;
    }
    If you can't do that then you have to allocate the memory yourself and free it in the calling function.
    Code:
    char *new_book()
    {
            char *fn = malloc( 104 );
            if( fn == NULL ) {
                    fprintf( stderr, "Couldn't allocate memory\n" );
                    return NULL;
            }
    
            printf( "What do you want your new address book to be called: " );
            scanf( "%s", &fn );
            strcat( fn, ".dg" );
    
            return fn;
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    make the array in main() or whereever and pass it into the function. arrays are passed by address so any changes you make to the array in your function will be available in main()

    for instance....

    Code:
    #include<stdio.h>
    
    void GetFileName(char[],int);
    
    int main()
    {
    int SIZE=256;
    char filename[SIZE]={0};
    GetFileName(filename,SIZE);
    printf("Your filename chosen is :- %s",filename);
    return 0;
    }
    
    void GetFileName(char filename[],int sizeofbuffer)
    {
    // use fgets to get input into filename
    // use strcat to concatenate .dz on end of filename
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  2. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

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