Thread: Returning Array

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Returning Array

    I am trouble returning a pointer to an array back to main:

    Code:
    int *funct(int argc, char *argv[]);
    main
    Code:
    int *a = funct (argc,argv);
    printf("%d\n ", a[1]);
    function
    Code:
    int *funct(int argc, char *argv[])
    {
      //need argc, argv inside this function 
      int arr[3] = {1,2,3};
      return *arr;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It makes no sense to return a pointer to data that no longer exists. Pass the array to a function.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    When I do that I get the same error as before : "function returns address of local variable"
    rhe error is caused by the return line
    Last edited by Suchy; 03-02-2008 at 03:39 PM.

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Of course because of the way local variables in functions are stored.
    They live on the stack, and stacks are very temporary things, so you can not return the address of a variable that lies on the stack.
    You will have to do something different.
    The array must be either dynamically allocated and initialized in the function or statically allocated on the heap as a global variable, or a static function member.
    Dynamic allocation is the best option, as you get a new copy with every call, but the caller must be then burdened with freeing the memory using a call to free.
    Code:
    int *getArray();
    
    int main()
    {
        int *array;
        
        array = getArray();
        array[1] = 4;
        free(array);
    
        return 0;
    }
    
    int *getArray()
    {
        int *a;
        
        a = malloc(3 * sizeof(*a));
        return a;
    }
    A small but complete example.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Suchy View Post
    When I do that I get the same error as before : "function returns address of local variable"
    rhe error is caused by the return line
    Show the code

    The array must be either dynamically allocated and initialized in the function or statically allocated on the heap as a global variable, or a static function member.
    Not it can be just function parameter - look at strcpy, it does nothing of the above and still fills the destination with some data and returns pointer to it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing and returning array pointers?
    By thealmightyone in forum C Programming
    Replies: 26
    Last Post: 03-26-2009, 03:38 PM
  2. Returning an Array
    By mmarab in forum C Programming
    Replies: 10
    Last Post: 07-19-2007, 07:05 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM