Thread: Confusion with passing Arrays

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    26

    Confusion with passing Arrays

    Hey all,

    I am having trouble with getting an array from a local function, for a local function. So i have....

    Code:
    int main(int argc, char *argv)
    {      code     }
    
    int local_function1()
    {      code     }
    
    int local_function2()
    {      code     }

    a line inside local_function1 makes a call to local_function2 which creates an array. This array then needs to be passed back to local_function1 for processing. local_function1 will make this call as many as 42 times, each time local_function2 will pass back a different array.

    I am having a great deal of trouble implementing this.

    If anyone has any pointers on this one i'd really appreciate it.

    Thanks,
    -Nick

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You can't return arrays (as such), though you can allocate and return a pointer.

    Or the simplest thing is for the caller to declare the array and pass this to the function to fill in.
    Much like say fgets() or strcpy() get passed an array of where to store the result.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    26
    Cool, so the return statement in local_function2 would be...

    Code:
    return array;
    would this direct the "filled in" array back to local_function1?

    Thanks again Salem,
    -Nick

  4. #4
    Registered User
    Join Date
    Apr 2006
    Location
    Plymouth, U.K.
    Posts
    13
    Sorry, this post is useless now.
    Last edited by Mindphuck; 04-04-2006 at 04:06 AM. Reason: Useless post compared to Salem's :(

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Except all that you return is a pointer to the array.

    And when the array itself goes out of scope when the function exits, then your pointer becomes a pointer to unallocated memory (this is bad).
    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.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    26
    Thanks for your help guys.

    I wrote out a quick program to test your advise before I attempted to implement it in my program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void func1();
    int func2(int *array);
    
    
    int main()
    {
       func1();
       return 0;
    }
    
    void func1()
    {
       int i;
       int array[10];
       func2(array);
       for (i = 0; i < 10; i++)
       {
          printf("%d\n", array[i]);
       }
    }
    
    
    int func2(int *array)
    {
       int i;
       for (i = 0; i < 10; i++)
       {
          array[i] = i;
       }
       return *array;
    }
    this printed out as expected....

    Code:
    [*** ~/ENEL323]gcc test.c -Wall -o test
    [*** ~/ENEL323]./test
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Thanks again,
    -Nick

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This would be better
    Code:
    void func2(int *array)
    {
       int i;
       for (i = 0; i < 10; i++)
       {
          array[i] = i;
       }
    }
    You don't do
    result = func2(array);
    so there is no point in returning anything if you're just going to throw the answer away.

    All the useful answers are stored in the array anyway.
    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.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    26
    Noted

    Cheers,
    -Nick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. More trouble passing arrays
    By Rad_Turnip in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 08:11 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM
  5. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM