Thread: function return values

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    32

    function return values

    Dear All,
    I am looking for some notes on function return values (array, pointer and char array).

    help me !


    Thanks.....

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use the return keyword. Ex.
    Code:
    int func()
    {
        return 666;
    }

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    Thats ok, its returning an integer, But I need some notes on function return vales for arrays , pointers and char arrays.


    Thanks............

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What kind of notes are you looking for? It's the same thing.
    Code:
    char* func()
    {
        char* buf = malloc( 80 );
        return buf;
    }
    
    int* func()
    {
        static int array[20];
        return array;
    }

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    Thats nice, Can u provide a example program on function returns pointer and function returns array.

    Thanks....

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The whole forum is littered with all sorts of program examples.

    Or post some of your own code, and ask how to turn some of it into a 'value returning function'. Seeing how it's done in a real example which means something to you will help you figure it out.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    The reason returning a pointer is different from returning some other type of data is that pointers will not extend the lifetime of the thing that they point to. So if you attempt to return a pointer to a local variable, the data that you need will likely be destroyed and you can't use it.

    So it all comes down to the scope of the pointer's pointed-to data.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    Does below code make any sense.....,

    I am trying to return an array from a function and print it.


    Code:
    #include <stdio.h>
    
    char* func()
    {
        char array[]=ashok;
        return array;
    }
    
    int main()
    {
    	char a[10];
    	a=func();
    	printf("array a=%s\n",a);
    	return 0;
    }

    help me to do so...



    Thanks...........

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ashok449 View Post
    Does below code make any sense.....,
    Not really.
    Quote Originally Posted by ashok449 View Post
    I am trying to return an array from a function and print it.


    Code:
    #include <stdio.h>
    
    char* func()
    {
        char array[]=ashok;
        return array;
    }
    
    int main()
    {
    	char a[10];
    	a=func();
    	printf("array a=%s\n",a);
    	return 0;
    }

    help me to do so...



    Thanks...........
    Let's break it down to pieces. When the compiler gets to char a[10]; it allocates a spot in memory large enough to hold 10 characters, and sets the variable "a" to point to that spot in memory.

    The function call then happens. The function allocates its own temporary array and notes its address. The address is returned, but when the function ends, the temporary array is destroyed. (Of course, your physical memory isn't literally destroyed; but the memory is no longer yours.) So we have this situation (ASCII art FTW):
    Code:
         +-+-+-+-+-+-+-+-+-+-+
    a    | | | | | | | | | | | (original memory)
     \   +-+-+-+-+-+-+-+-+-+-+
      \
       --->  undefined land
    So you have the original memory that was allocated, but you can't get there anymore. a is now a pointer to memory that has been freed, which is illegal to use; so when you attempt to use it in the next line, you will be arrested.[1]

    The customary idiom here is to pass the pointer into the function; the function then writes into the memory location pointed to by the pointer. (Be sure to pass in the size of the memory too!) If you feel that you must return a pointer, you have to malloc() it in the function; memory that you explicitly create will last until you explicitly destroy it with free(), unlike the stuff that is automatically created for you (such as arrays), which are automatically destroyed for you when they go out of scope.

    (Also: string literals must go in "quotes".)

    [1] This is not really true; it is undefined, however, so your system is free to do whatever it pleases. And what pleases your system may not please you.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    From what i can see from your previous post, i think you are after something which is similar to the following code. Have a look, i the func function takes char array and copies some value and exits the function with out returning a value.

    But the function foo return a char *, cos the memory was allocated in the function and the address of the memory location which was allocation is returned to the main function. And also make a note of free function. Since i explicitly allocated memory using malloc, it the programmer job to free it.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void func(char *str)
    {
        strcpy(str, "Ashok");
    }
    
    char *foo(void)
    {
         char *str;
         
         str = malloc( sizeof(char) * 10 );
         
         if(str != NULL)
             strcpy(str, "Hello");
         else
             return NULL;
         
         return str;
    }
                       
    int main()
    {
        char a[10];
        char *p = foo();
        
        func(a);
    
        if( p != NULL )
            printf("array a = &#37;s %s\n", p, a );
        else
            printf("array a = %s\n", a);
            
        free(p);              
        getchar();
    	return 0;
    }
    
    /* my output
    array a = Hello Ashok
    */
    ssharish

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    32
    Thats great ... Thank you very much

    It helped me alot to understand all these.


    Thanks ssharish.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM