By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,014 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,014 IT Pros & Developers. It's quick & easy.

how to return an array from function

P: n/a
Hi mates

I want to know a simple program of return array from function ? Do I
need to use pointer to return the address of the first element in an
array.

Isaac
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In article <88**************************@posting.google.com >, Isaac wrote:
Hi mates

I want to know a simple program of return array from function ? Do I
need to use pointer to return the address of the first element in an
array.

#include <stdlib.h> /* for malloc(), free(), and NULL */

int *int_arr_allocator(int length)
{
int *arr;

arr = malloc(length*sizeof *arr);
if (arr == NULL) {
/* handle error */
}

return arr;
}

int main(void)
{
int *myarr;

/* allocate an array of length 10 */
myarr = int_arr_allocator(10);

/* use the array */

/* free the array */
free(myarr);

return 0;
}
--
Andreas Kähäri
Nov 13 '05 #2

P: n/a
fp*****@yahoo.co.uk (Isaac) wrote in message news:<88**************************@posting.google. com>...
Hi mates

I want to know a simple program of return array from function ? Do I
need to use pointer to return the address of the first element in an
array.

Isaac


Hi Isaac,

Besides the technique already posted, which returns a pointer to a
malloced array, there is another simple trick often used to return an
array from a function. It goes this way.

[code not tested]

struct retarr {
char arr_to_be_ret[20]; /* May be nitpicked :) Better still, use the
preprocessor */
};
struct retarr ArrayReturner(void);

int main(void) {
struct retarr arrcollect;
arrcollect=ArrayReturner();
/* Do whatever you want with the returned array
arrcollect.arr_to_be_ret[] */
/* For what to return here, look at the other threads , its not
quite trivial.
However for all purposes, just letting the main fall off is good
enough
for the standard */
}
struct retarr ArrayReturner(void) {
struct retarr arrobj;
int i;
for(i=0;i<20;i++) { /* Here's why the above comment was valid
i<(what) */
arrobj.arr_to_be_ret[i]=i; /* Or whatever you want to do with the
array */
}
return arrobj;
}

Regards,
Anupam
Nov 13 '05 #3

P: n/a
fp*****@yahoo.co.uk (Isaac) wrote in message news:<88**************************@posting.google. com>...
Hi mates

I want to know a simple program of return array from function ? Do I
need to use pointer to return the address of the first element in an
array.

Isaac


This covers most of the possible variations. The first two methods
are the most common (using pointers to the base type, rather than
pointers to arrays of the base type).

#include <stdlib.h>
#include <stdio.h>

/*
** Allocate space to hold _size_ ints, return pointer to
** base address as function return value. This is the
** most common method.
*/
int *newa1 (size_t size)
{
int *ap;

ap = malloc (sizeof *ap * size);
if (!ap)
{
printf ("newa1: malloc failed\n");
}
return ap;
}

/*
** Allocate space to hold _size_ ints, return pointer to
** base address as function parameter
*/
void newa2 (size_t size, int **ap)
{
*ap = malloc (sizeof **ap * size);
if (!*ap)
{
printf ("newa2: malloc failed\n");
}
}

/*
** Allocate space for fixed-size array of int, return pointer
** to array as function return value. Not generally used,
** since array sizes are fixed and must be known ahead of time.
**
** The declaration reads as
**
** newa3 -- newa3
** is a function -- newa3()
** taking no parameters -- newa3(void)
** returning a pointer -- *newa3(void)
** to a 10 element array -- (*newa3(void))[10]
** of int -- int (*newa3(void))[10]
*/
int (*newa3 (void))[10]
{
int (*ap)[10];

ap = malloc (sizeof *ap);
if (!*ap)
{
printf ("newa3: malloc failed\n");
}
return ap;
}

/*
** Allocate space for a fixed-size array of int, return pointer
** to array as function parameter
*/
void newa4 (int (**ap)[20])
{
*ap = malloc (sizeof **ap);
if (!*ap)
{
printf ("newa4: malloc failed\n");
}
}

int main (void)
{
int *a1, *a2; /* a1 and a2 are pointers to int */
int (*a3)[10], (*a4)[20]; /* a3 and a4 are pointers to arrays of
int */
int i;

a1 = newa1 (10); /* function call */
if (a1) /* test */
a1[0] = 0; /* assignment */

newa2 (20, &a2);
if (a2)
a2[1] = 1;

a3 = newa3 ();
if (a3)
(*a3)[2] = 2;

newa4 (&a4);
if (a4)
(*a4)[3] = 3;

return 0;
}

Important things to remember:

a1, a2 -- pointers to int.
*a1, *a2 -- int values
a3, a4 -- pointers to arrays of int
*a3, *a4 -- arrays of int values

The spaces that a1 and a2 point to may be resized using realloc().
The spaces pointed to by a3 and a4 may not be resized.

*a1 = 0; /* legal, equivalent to a1[0] = 0; */
*a3 = 0; /* illegal; *a3 is an array type, not an int type */

IME, using pointers to arrays (a3 and a4) is not very common, since
the primary reasons you're using dynamic memory are a) you don't know
how much space you'll need until runtime, and b) you want to be able
to resize that space as needed. However, I thought I'd toss that in
for completeness' sake.
Nov 13 '05 #4

P: n/a
In <88**************************@posting.google.com > fp*****@yahoo.co.uk (Isaac) writes:
I want to know a simple program of return array from function ? Do I
need to use pointer to return the address of the first element in an
array.


Show us how you allocate the array, first.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.