Functions that return arrays. 
Author Message
 Functions that return arrays.

I would like to do the following with a piece of code:

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

XXXX ThisFunction();

int main (void)
{
    int new_array[20];
    new_array = ThisFunction();

Quote:
}

XXXX ThisFunction ()
{
    int this_array[20];
    int i = 0;

    for (i = 0; i < 20; i++)
          this_array = 10;

    return (this_array);

Quote:
}

Is this possible to do?  I haven't found a syntax that will work for
this.  If this isn't possible, is there another way that I should look
into doing this?

Jeff Hanks

(remove the X from my email address to reply)



Sun, 22 Apr 2001 03:00:00 GMT  
 Functions that return arrays.

Quote:

>I would like to do the following with a piece of code:

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

>XXXX ThisFunction();

>int main (void)
>{
>    int new_array[20];
>    new_array = ThisFunction();
>}

>XXXX ThisFunction ()
>{
>    int this_array[20];
>    int i = 0;

>    for (i = 0; i < 20; i++)
>          this_array = 10;

>    return (this_array);
>}

>Is this possible to do?  I haven't found a syntax that will work for
>this.  If this isn't possible, is there another way that I should look
>into doing this?

No, it's not possible.  Functions may not return arrays, and arrays may not be
assigned.  There are two alternatives, one much more efficient than the other.

1(preferred): Pass the address of an array, and have the function fill it in.
void ThisFunction(int *array)
{
   int i = 0;
   for (i = 0; i < 20; i++)
      array[i] = 10; /* Note the [i] that you forgot above */

Quote:
}

int main(void)
{
   int main_array[20];
   ThisFunction(main_array);

Quote:
}

2: Wrap the array in a structure.  Structures may be returned and assigned:
typedef struct { int [20]a; } arraystruct;
array_struct ThisFunction(void)
{
   int i;
   array_struct as;
   for (i = 0; i < 20; i++) as.a[i] = 10;
   return as;
Quote:
}

int main(void)
{
   array_struct new_array;
   new_array = ThisFunction();
Quote:
}

--

Kenan Systems Corporation


Sun, 22 Apr 2001 03:00:00 GMT  
 Functions that return arrays.

On Wed, 04 Nov 1998 11:10:18 -0800, Jeffrey Hanks

Quote:

>I would like to do the following with a piece of code:

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

>XXXX ThisFunction();

>int main (void)
>{
>    int new_array[20];
>    new_array = ThisFunction();
>}

The problem here is that a return value for a function in C will only
return a value(i.e a char, int, pointer, ...).  Here you are trying to
copy the values in one block of memory(i.e. an array) to another block
just by useing the equal sign ... not going to happen.
Quote:

>XXXX ThisFunction ()
>{
>    int this_array[20];
>    int i = 0;

>    for (i = 0; i < 20; i++)
>          this_array = 10;

>    return (this_array);
>}

>Is this possible to do?  I haven't found a syntax that will work for
>this.  If this isn't possible, is there another way that I should look
>into doing this?

>Jeff Hanks

>(remove the X from my email address to reply)

Here is one solution to the problem, but it may not be exactly what
you are looking for:

#include <stdio.h>
#define ARRAYSIZE 10

int* ThisFunc(int arraysize)
{
        int *ip_newarray;
        int i;

        if(!(malloc(sizeof(int) * arraysize)) return(NULL);
        for(i = 0;i < arraysize;ip_newarray[i++] = 10);
        return(ip_newarray);

Quote:
}

main()
{
        int *new_array;
        new_array = ThisFunc(ARRAYSIZE);
        if(new_array == NULL) {
                printf("Memory is cheap: Why don't I have any!\n");
                return(-1);
        }
        return(0);

Quote:
}

This routine creates the array.  Another option is to copy from one
array to the other.  I belive I already saw a post that does that, so
I will not recreate the code here.
Robert Sills
Senior Systems Programer
Netrex Inc.


Mon, 23 Apr 2001 03:00:00 GMT  
 Functions that return arrays.
...

Quote:
>    int *ip_newarray;
>    int i;

>    if(!(malloc(sizeof(int) * arraysize)) return(NULL);
>    for(i = 0;i < arraysize;ip_newarray[i++] = 10);
>    return(ip_newarray);

And here is an excellent example of the dangers of trying to code for
conciseness instead of for clarity.  Quick: what's the effect of the for() loop?
--

Kenan Systems Corporation


Tue, 24 Apr 2001 03:00:00 GMT  
 Functions that return arrays.

Quote:

>...
>>        int *ip_newarray;
>>        int i;

>>        if(!(malloc(sizeof(int) * arraysize)) return(NULL);
>>        for(i = 0;i < arraysize;ip_newarray[i++] = 10);
>>        return(ip_newarray);

>And here is an excellent example of the dangers of trying to code for
>conciseness instead of for clarity.  Quick: what's the effect of the
>for() loop?

It dereferences an uninitialized pointer?  It otherwise looks ok to
me.  But, here's how I would have written the snippet for those who
care.

        int *ip_newarray;
        int i;

        ip_newarray = malloc (sizeof (int) * arraysize);

        if (ip_newarray)
          for (i = 0; i < arraysize; i++)
            ip_newarray[i] = 10;

        return ip_newarray;

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Tue, 24 Apr 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Functions that return arrays (newbie)

2. Functions that return arrays

3. Function to return array of strings

4. Pointer to function returning pointer to function returning...

5. Returning an array of int from a function

6. Returning a 2D array from a function...

7. Returning pointer to array of structure from function

8. How to make a function return an array?

9. Function returning an array

10. Arrays, Functions, and Returning Pointers via Parameters

11. Help: returning an array from a function.

12. function returning pointer array

 

 
Powered by phpBB® Forum Software