Thread: Function returning a structure and function returning a string-- Different behaviour?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27

    Question Function returning a structure and function returning a string-- Different behaviour?

    Hi,

    I have heard that we cannot return a pointer to a local (automatic) array variable from a function since the automatic variable have limited scope and they die off once the called function returns.

    Code:
    char *s getName()
    {
    char name[]="Sumit";  // Automatic variable 
    retrun name; // No scope outside the function
    
    }
    But a doubt arises in my mind:

    Code:
    struct info getInfo(int a,int b)
    {
    
    struct info f1; // Automatic variable memory allocated for a structure
    f1.a=a;
    f1.b=b;
    
    return f1;
    
    }
    here also we are returning a reference to a memory location which is allocated locally. Then how come here it works perfectly fine.


    PLZ HELP
    Waiting for reply

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are not returning a reference to a memory location, so the analogy fails. (If you were returning &f1, then you would be in the same world of hurt that you were in before.)

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    As tabstop points out ... C cannot return arrays. It can return a struct, because it knows how big it is. (Well, actually it's returning a copy of a struct) Of course it can return a string in a struct...

    Here's an example to demonstrate why you should never try to retun arrays from functions in C...

    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }
    Try it both with and without the static keyword...

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27
    @tabstop: Thnnx for ur reply.
    But another doubt here:
    Why cant we return a pointer to that memory allocated although we are able to return the memory chunk itself?

    The memory allocated in both cases (in case of an array and in case of a structure) are local to the function.
    The how come it is getting retained in structure case and gets destroyed in case of an array?



    Quote Originally Posted by tabstop View Post
    You are not returning a reference to a memory location, so the analogy fails. (If you were returning &f1, then you would be in the same world of hurt that you were in before.)

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just as a copy of int a and int b are made going in to your function, a copy of the struct (or whatever is returned) is made coming out. (If you return a pointer, the pointer is copied, but that doesn't help because the address it points to is no longer valid.)

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27

    Thumbs up

    Quote Originally Posted by tabstop View Post
    Just as a copy of int a and int b are made going in to your function, a copy of the struct (or whatever is returned) is made coming out. (If you return a pointer, the pointer is copied, but that doesn't help because the address it points to is no longer valid.)
    Thank you very much for clearing the the things...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning a string from a function
    By budala in forum C Programming
    Replies: 4
    Last Post: 01-18-2010, 10:03 PM
  2. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  3. Problem with function returning a structure
    By SkaxCo in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2007, 12:27 PM
  4. returning a string from a function
    By revelation437 in forum C Programming
    Replies: 6
    Last Post: 12-14-2002, 04:21 AM
  5. returning a string from a function
    By itld in forum Linux Programming
    Replies: 5
    Last Post: 12-03-2001, 01:35 AM

Tags for this Thread