Thread: How to return an array from a function

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    12

    How to return an array from a function

    I'm relatively new to C and I'm having trouble returning an array from a function. Apologies if this have been answered elsewhere but I can't find it. I read somewhere about returning the first element and using a pointer to find the the other elements.

    A simplified version of my code is:

    Code:
    int main()
    {
      int array[3];
      array[0] = func(); 
    }
    
    int func()
    {
      static int array[3];
      array[0]=5;
      array[1]=6;
      array[2]=7;
    
      return array[0];
    }
    This returns the first element but I don't know how to return the other elements.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    Dnipropetrovsk, Ukraine
    Posts
    25
    Hi,
    I'm new to C as well. Sorry if my suggestion turns out to be wrong.
    As far as I understand, for a function to return an array it should be declared as returning a pointer to a desired type, in your code to a type int. (e.g int * func(void))
    The return statement should be something like (return array). The name of an array is a pointer to the 1st element of an array.
    It probably defies the purpose of your task, but you may want to consider the following code:

    Code:
    #include <stdio.h>
    
    #define SIZE 3
    
    /* function prototype; ar[] being a pointer to type int;
     * n being array size */
    void func(int ar[], int n);
    
    int main(void)
    {
    	int i;	/* counter variable */
    
    	/* declare an array of 3 ints */
    	int array[SIZE];
    
    	/* call func() to initialise array[]; pass array name, itself a pointer and
             * an array size */
    	func(array, SIZE);
    
      	/* print contents of array */
    	for (i = 0; i < SIZE; i++)
    		printf("%d ", array[i]);
    	printf("\n");
    
    	return 0;
    }
    
    void func(int ar[], int n)
    {
    	int i;
    	for (i = 0; i < n; i++)
    		ar[i] = i + 5;	/* any other numbers desired */
    }
    You may also want to see this link for an answer to how to return arrays from functions.
    Last edited by sashaKap; 01-26-2010 at 06:42 AM.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You can return a pointer to the array. If the array is not static within the function, you must return a dynamically allocated array instead.

    Code:
    int *func_static(void)
    {
            static int arr[5] = {0};
            return arr;
    }
    
    int *func_dynamic(void)
    {
            int *arr = malloc(5 * sizeof(int));
            return arr;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course, if you use dynamic memory as in the second example, you must free it. Therefore, the preferred way of doing this is doing it like in sashaKap's example.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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