Thread: return array from function

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    return array from function

    Hi,

    I'm currently doing the following in my app
    Code:
    int funcGetArrayLength()
    return len
    
    char *arr[len]
    I then need to perform some actions on this newly created array using another function. Is the best way forward returning an array that I can copy into my original? Any responses greatly appreciated.

    Thanks
    Last edited by beginner.c; 04-26-2007 at 04:53 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You are aware that its an array of pointers yes?

    Some implementations, edit the array as a argument;

    eg
    Code:
    void foo(char * array)
    {
        array[0] = 'a';
        return;
    }
    But perhaps providing a better example of what your trying to do could help?

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Not sure what you're really doing in your code, but if you're talking about creating an array inside of a function and trying to return it, here are a few words on the subject.

    First of all, returning an array is basically just returning a pointer to the first element.

    Right:

    Code:
    int *createArray(int iLength)
    {
    	int *array = malloc(iLength * sizeof(*array)); /* Make sure it's dynamic memory,
                                                 NOT local to the function! */
    	return array;
    }
    malloc() has not been casted as a favor to those that freak out over it.

    WRONG:

    Code:
    int *createArray(int iLength)
    {
    	int array[iLength]; /* WRONG WRONG WRONG */
    	return array; /* WRONG WRONG WRONG */
    }
    The reason the last example is wrong is because local variables are destroyed upon the return of the function. In the first example, the memory was located from the heap while local variables are allocated on the stack.

    Edit: Corrected mistake, missing sizeof(). My sincerest apologies.
    Last edited by MacGyver; 04-26-2007 at 05:18 AM. Reason: Corrected mistake, missing sizeof(). My sincerest apologies.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > int *array = malloc(iLength * (*array));
    You mean
    int *array = malloc(iLength * sizeof(*array));
    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.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I knew I should have gotten some sleep. Yes, you are correct, as usual. My sincerest apologies.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Thanks for prompt replies.

    What I'm doing

    Run a function to get the number of files in a directory (return int numFiles)
    Create my array for storing the filenames
    Run another function to actually store the filenames in the array.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well it's kinda silly doing it that way... Why not just have one function that does all of that ? :|

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Yes well now that I know how to return an array I'll definitely do that. It also seems like twice the work searching the directory twice (once for the size of the array and then again to store the filenames). Can I do this dynamically by only searching the dir once??

    Don't expect you to do my homework.....but if it's possible (and not too involved) I'd love to know..
    Last edited by beginner.c; 04-26-2007 at 05:29 AM.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, you can do that. I wrote a function just recently that did something similar.

    Here's a general idea:

    1) Create an array of an initial size.
    2) In a loop, read an element (file name in your case?) and stick it in the array.
    3) If you run out of memory in your array, call realloc() and make it bigger. If realloc() fails, you pretty much ran out of memory in your system.
    4) If you run out of elements, you're done.
    5) Call realloc() to resize the array to trim off any extra space. Optional if you're more concerned about performance instead of memory.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Perhaps a struct, with a linked list?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXPATH 260
    
    struct filenode_t {
    	struct filenode * next;
    	char FileName[MAXPATH];
    } filenode;
    
    struct contents_t {
    	struct filenode * f;
    } contents;
    
    int GetDirContents(struct contents * c);
    
    int main(void)
    {
    	int files = 0;
    	struct contents * c;
    
    	files = GetDirContents(c);
    
    	// remember to free the linked list!
    	return 0;
    }
    
    int GetDirContents(struct contents * c)
    {
    	int nFiles = 0;		// num of files found
    
    	// scan / malloc directory contents...
    
    	// if it fails return -1 files ? (So you know it failed??)- upto you.
    
    	return nFiles;
    }
    Dunno if this is the best way to do it?

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Great thanks for the tips. I knew it would involve realloc() somwhere.

    Thanks everybody.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    *mutters something about this very scenario being in the FAQ*
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM