home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!uunet.ca!canrem!telly!druid!darcy
- From: darcy@druid.uucp (D'Arcy J.M. Cain)
- Subject: Re: Arrays with variable dimensions?
- Message-ID: <1992Dec31.104644.9299@druid.uucp>
- Date: Thu, 31 Dec 1992 10:46:44 GMT
- References: <1992Dec26.220736.22177@news.ysu.edu> <C02pwu.93C@world.std.com>
- Organization: D'Arcy Cain Consulting
- Lines: 41
-
- dirsolns@world.std.com (Bernard Farrell) writes:
- >ah017@yfn.ysu.edu (John B. Lee) writes:
- >: Is it possible to create an array that would change size according
- >: to user input?
- >float fl_arr[] = NULL; /* This is actually a float * -- no space */
-
- You didn't even try this did you? This actually does assign space to an
- array. I don't know what your compiler does with the "= NULL" but assuming
- it even accepts it (mine doesn't) it certainly doesn't do what you think it
- does. If you want a float * why didn't you just declare that:
-
- float *fl_arr = NULL;
-
- I would do the rest slightly different than your example but with the above
- change your code should work.
-
- >This allows the array to increase in size as required. The
- >alternative is to do a malloc(), instead of using realloc(),
- >based on a value given at run-time. Note that the above
- >code works only when realloc() works as malloc() when the
- >first parameter is NULL -- some implementations of realloc()
- >do not have this behavior.
-
- If you can count on an ANSI compiler you are fine but using a malloc first
- is safer if you can't be sure. Perhaps something like this at the start
- of the code:
-
- #ifndef __STDC__
- if ((fl_arr = malloc(sizeof(*fl_arr) * ARRAY_INCR)) == NULL)
- print_horrible_error_and_exit();
- fl_max = ARRAY_INCR;
- #endif
-
- Of course if you are really paranoid about the __STDC__ constant you'll
- just do this every time regardless.
-
- --
- D'Arcy J.M. Cain (darcy@druid.com) |
- D'Arcy Cain Consulting | There's no government
- Toronto, Ontario, Canada | like no government!
- +1 416 424 2871 DoD#0082 |
-