home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19067 < prev    next >
Encoding:
Text File  |  1992-12-31  |  2.0 KB  |  52 lines

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