Returning Array

This is a discussion on Returning Array within the C Programming forums, part of the General Programming Boards category; I am trouble returning a pointer to an array back to main: Code: int *funct(int argc, char *argv[]); main Code: ...

  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,006
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 04:38 PM
  2. Returning an Array
    By mmarab in forum C Programming
    Replies: 10
    Last Post: 07-19-2007, 08:05 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Replies: 6
    Last Post: 10-21-2003, 10:57 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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