tobold.org

correct • elegant • free

△ Comp.lang.c △

◅ How can I determine if a number is even?

Need Bubble Sort algorithm... ▻

HELP! Are dynamically allocated static arrays possible?

In article <474042$hsp@decaxp.harvard.edu>,
Zorro  <berriz@husc.harvard.edu> wrote:
>                                         Is there any way to define an
>array that is both dynamically allocated and static to the function?

More or less.  Suppose it's a bunch of `double's you want, with
the size of the bunch being a parameter to the function the first
time it is called...

    void foo(int size) {
            static double *bunch = 0;

            if (!bunch) {
                    bunch = malloc(size * sizeof double);
                    if (!bunch) {
                            /* deal with malloc failure */
                    }
            }

            /* access the doubles using array notation...

                    bar = bunch[n];

            or pointer notation...

                    bar = *(bunch + n);
            */
    }

Note that the size parameter is ignored on all but the first call
to foo(); you could use realloc() if this is not what you want.

Tim.
--
Tim Goodwin   | "... which works fine, but from a language design aesthetics
Unipalm PIPEX | point of view (i.e., mine), is grotesque." -- Chris Torek

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.c
Subject: Re: HELP! Are dynamically allocated static arrays possible?
Date: 31 Oct 1995 13:22:49 GMT
Organization: Unipalm PIPEX
Message-ID: <4757v9$87l@wave.news.pipex.net>
References: <474042$hsp@decaxp.harvard.edu>

△ Comp.lang.c △

◅ How can I determine if a number is even?

Need Bubble Sort algorithm... ▻