Question:
Returning array from a function in C?
?
2012-05-26 01:23:20 UTC
Hi guys Im trying to write a program and within it a function needs to return an array...

like return whatever; where whatever contains an array/string like abc, adfasd .....

now what will be the
1.Function prototype
2.what will b the passing (to the function from main) mechanism
3. what will be the function definition
4. And what will be the return statement...

it will b better if u answer with the help of an example..... Thanks
Four answers:
AnalProgrammer
2012-05-26 01:48:38 UTC
Passing arrays to functions

http://www-ee.eng.hawaii.edu/~tep/EE160/Book/chap7/section2.1.2.html



Returning Arrays from functions

http://www.eskimo.com/~scs/cclass/int/sx5.html



Have fun.
anonymous
2012-05-26 08:36:49 UTC
You cannot return arrays in C. You can return structs and unions with arrays as members, or pointers to arrays, but not entire arrays.



Consider using prototype similar to the following:

void f(int *array, size_t size);



Then call it like so:

int a[10];



f(a, sizeof a / sizeof a[0]);



With this method you pass a pointer to the first element of the array as well as the size of the array to f. f then operates on up to 'size' members from 'array'. This is the method used by many functions in the standard C library.
husoski
2012-05-26 09:25:35 UTC
You can't return an array object as a function result in C (nor in C++, for that matter).



Some alternate methods:



1. (most common) Have the caller provide the array for the return value as an argument. The called function fills in the array. For safety, both must agree on the maximum size of the array. If the size varies from one call to another, it's imperative to pass the actual length of the receiving array in another argument. The standard library function fgets() in is a good example of this.



2. Have the called function use malloc() or calloc() to dynamically allocate storage for the array, then return a pointer to this array as a function result. Returning pointers is OK in C. It is the calling function's responsibility to use free() to release the memory when the array is no longer needed.



3. Wrap the array in a struct, and return the struct. Struct return values are OK in C. This requires a fixed-size array, so every return value will occupy the maximum size defined in the struct. This is very inefficient for highly variable result sizes, but can be very nice if there's a fixed maximum size for all callers, and the actual results are near enough to the maximum for most calls.



Examples of all three:



#include

#include

#include



#define MAX_STRLEN 80

typedef struct stringresult

{

char s[MAX_STRLEN+1];

} stringresult;



/* Option 1: caller provides buffer */

void getString(char *result, size_t length)

{

strncpy(result, "Hello Caller!", length);

result[length-1] = '\0';

}



/* Option 2: called function allocates buffer: */

char *getDynamicString()

{

static char message[] = "Remember to free() me!";

char *result = (char*)malloc(strlen(message)+1);

if (result != NULL)

strcpy(result,message);

return result;

}



/* Option 3: return stringresult struct */



stringresult getStructString()

{

stringresult result;

strncpy(result.s, "Goodbye now.", sizeof result.s);

return result;

}



int main()

{

char astring[20];

char *dstring;

stringresult sstring;



getString(astring, sizeof astring);

printf("getString returned: %s\n", astring);



dstring = getDynamicString();

printf("getDynamicString returned: %s\n", dstring);

free(dstring);



sstring = getStructString();

printf("getStructString returned %s\n", sstring.s);



return 0;

}
James Bond
2012-05-26 09:23:14 UTC
You can not return an array from a function like java. However, you can create a dynamic array in the function and then return its address after storing required values



int * xxx(...........)

{

int *p;



p=(int *) malloc(...);

store values in p

return p;

}



int main()

{

int *x;



x=xxx(..);

x[0], x[1], etc are the values returned by function xxx


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...