Functions that return arrays

This is a discussion on Functions that return arrays within the C Programming forums, part of the General Programming Boards category; Hi guys. I'm writing a function for my program. This function is required to return an array or at least ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Functions that return arrays

    Hi guys.

    I'm writing a function for my program.
    This function is required to return an array or at least a pointer to an array.
    The array is 2 dimensional.
    How do i do this in the function, and how do i call the function in the mainline?

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Perhaps you could try this:
    Code:
    int** foo(int x, int y)
    {
          int** arr = malloc ( x * sizeof( *arr ) ); // allocate memory for rows
          int i;
          for ( i = 0; i < x; i++ )  // for every row allocate y cells
              arr[i] = malloc ( y * sizeof( *arr[i] ) );
          
          return arr; 
    }
    ... and calling this in main function will look like
    Code:
    int** myArray = foo(5, 5);
    ...which creates 5x5 matrix. Don't forget to free memory once you're done with it.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Quote Originally Posted by Scarecrowm View Post
    Hi guys.

    I'm writing a function for my program.
    This function is required to return an array or at least a pointer to an array.
    The array is 2 dimensional.
    How do i do this in the function, and how do i call the function in the mainline?
    You don't. You write the function to take a 2D stack array or 2D dynamic array. You modify the array through the argument.

    The two prototypes look like this.
    Code:
    //ROWS should be a constant 
    int takeStaticArray(int cols,int array[][ROWS] );
    int takeDynamicArray(int cols, int rows, int **array);
    In each case accessing the array is done using brackets, like this: array [1][4].
    The later is preferred, but the former is uses if you declare a 2d array in a function (as opposed to using malloc() or calloc() ).

    Don't do what jimzy said, because it's bad style; any call to malloc() should be matched by a call to free() in the same function. It will work if you call free() later outside the function, but its easy to mess up, so it should be avoided whenever possible.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Why should we use free()? what is it that we have to free?

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by ozumsafa View Post
    Why should we use free()? what is it that we have to free?
    If you allocate any memory in your program at run time it should be freed, so that it can be used by other program. Hence it is very important to free he allocated memory

    ssharish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM

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