Returning an array of int from a function 
Author Message
 Returning an array of int from a function

Can anyone please provide an example of how to return an array of
integers from a function in ANSI C?

I'll assume you use a pointer to memory and that you would also use
malloc to dynamically allocate memory so that you won't be pointing to a
variable of limited scope and duration.  I'm a little fuzzy on exactly
how this should look...

I would appreciate your help and insight.

 - Ken



Sun, 15 Oct 2000 03:00:00 GMT  
 Returning an array of int from a function



Quote:
> Can anyone please provide an example of how to return an array of
> integers from a function in ANSI C?

> I'll assume you use a pointer to memory and that you would also use
> malloc to dynamically allocate memory so that you won't be pointing
to a
> variable of limited scope and duration.  I'm a little fuzzy on
exactly
> how this should look...

> I would appreciate your help and insight.

>  - Ken

<Jack>

Hi Ken, try this:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
int *make_array(size_t size);
int main(void)
{
        int count;
        int *ip = make_array(SIZE);
        if (!ip)
        {
                puts("Error: couldn't make array");
                return EXIT_FAILURE;
        }
        for (count = 0; count < SIZE; ++count)
        {
                printf("array[%d]=%d\n", count, ip[count]);
        }
        return EXIT_SUCCESS;

Quote:
}

int *make_array(size_t size)
{
        int count;
        int *pi = malloc(size);
        if (pi)
        {
                for (count = 0; count < size; ++count)
                pi[count] = count;
        }
        return pi;
Quote:
}



Sun, 15 Oct 2000 03:00:00 GMT  
 Returning an array of int from a function

Jack,

Thanks for the help!  You gave me just what I needed -- a concrete
example.  One thing I did notice on my implementation is that I got a
compiler warning on the  malloc  statement complaining of not having a
cast (although the program ran fine).  After modifying to   (int
*)malloc  , the warning vanished.

Again, thank you for your help!
 - Ken

Quote:

> Hi Ken, try this:

> #include <stdio.h>
> #include <stdlib.h>
> #define SIZE 10
> int *make_array(size_t size);
> int main(void)
> {
>         int count;
>         int *ip = make_array(SIZE);
>         if (!ip)
>         {
>                 puts("Error: couldn't make array");
>                 return EXIT_FAILURE;
>         }
>         for (count = 0; count < SIZE; ++count)
>         {
>                 printf("array[%d]=%d\n", count, ip[count]);
>         }
>         return EXIT_SUCCESS;
> }

> int *make_array(size_t size)
> {
>         int count;
>         int *pi = malloc(size);
>         if (pi)
>         {
>                 for (count = 0; count < size; ++count)
>                 pi[count] = count;
>         }
>         return pi;
> }



Sun, 15 Oct 2000 03:00:00 GMT  
 Returning an array of int from a function


Quote:
> Jack,
> Thanks for the help!  You gave me just what I needed -- a concrete
> example.  One thing I did notice on my implementation is that I got a
> compiler warning on the  malloc  statement complaining of not having a
> cast (although the program ran fine).  After modifying to   (int
> *)malloc  , the warning vanished.

You must have an older compiler, then, as the cast to (int *) is not
necessary in standard C; malloc returns void *, which is implicitly
converted.

Kurt Wall

--
c.l.c. FAQ, text & hypertext:
Text ftp://rtfm.mit.edu/pub/faqs/C-faq/faq
Text ftp://rtfm.mit.edu/pub/usenet-by-group/comp.lang.c/C-FAQ-list
Hypertext http://www.eskimo.com/~scs/C-faq/top.html



Sun, 15 Oct 2000 03:00:00 GMT  
 Returning an array of int from a function



Quote:
>Jack,

>Thanks for the help!  You gave me just what I needed -- a concrete
>example.  One thing I did notice on my implementation is that I got a
>compiler warning on the  malloc  statement complaining of not having a
>cast (although the program ran fine).  After modifying to   (int
>*)malloc  , the warning vanished.

In ANSI C a cast is unnecessary and best avoided. The compiler warning
could mean one of several things:

1. malloc() hasn't been correctly declared. Make sure the code you compiled
   really does have #include <stdlib.h>.

2. You are using a very old compiler/library which doesn't declare malloc
   properly or declares it as returning char * instead of the correct ANSI
   void * return. In that case you really should consider getting a better
   compiler. Trying to learn C on an old compiler is going to cause more
   problems than it is worth.

3. You aren't using a C compiler. C++ compilers would complain about the
   conversion. Always be clear what language you are writing in. If it is
   C make sure your compiler is configured as a C compiler, if C++ you
   would be better off using C++ constructs like new.

While ANSI C compilers could warn about an implicit conversion from void *
to int * (they can warn about anything they like) it is extremely unlikely
that one would, at leastnot without some bizarre command line options.

--
-----------------------------------------


-----------------------------------------



Sun, 15 Oct 2000 03:00:00 GMT  
 Returning an array of int from a function

Quote:

> Jack,

> Thanks for the help!  You gave me just what I needed -- a concrete
> example.  One thing I did notice on my implementation is that I got a
> compiler warning on the  malloc  statement complaining of not having a
> cast (although the program ran fine).  After modifying to   (int
> *)malloc  , the warning vanished.

Hi Ken Waters,

Might you be using your compiler as a C++ compiler ? But it should
be an error in this case. No, it is more likely that you are using
an old style K&R C compiler, right ? This is a compiler that predates
the ANSI-C standard. In ANSI-C the "void*" that "malloc()" returns
converts *automatically* to any other data object pointer type.

Indeed it is considered good programming style (in ANSI-C) *not*
to cast the return value of "malloc()", because this can hide the
error of forgetting to include <stdlib.h>.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 15 Oct 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Returning an array of int from a function

2. Returning Int array pointers from functions

3. Convert C++ (return type int, long, user-define, etc) function into COM/ATL

4. Calling int f(int (*f)(int)) like function in DLL from VB

5. Calling problem with function returning int

6. mem_fun not work on VC6 with function return int

7. Pointer to function returning pointer to function returning...

8. int pointer to int array

9. int to int array problem

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

11. Returning pointer to array of structure from function

12. How to make a function return an array?

 

 
Powered by phpBB® Forum Software