home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 19088 < prev    next >
Encoding:
Internet Message Format  |  1992-12-31  |  3.7 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!rpi!crdgw1!rdsunx.crd.ge.com!bart!volpe
  2. From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Arrays with variable dimensions?
  5. Message-ID: <1992Dec31.192950.26423@crd.ge.com>
  6. Date: 31 Dec 92 19:29:50 GMT
  7. References: <1992Dec31.104644.9299@druid.uucp> <C04tnH.LCH@world.std.com>
  8. Sender: volpe@bart (Christopher R Volpe)
  9. Reply-To: volpe@ausable.crd.ge.com
  10. Organization: GE Corporate Research & Development
  11. Lines: 71
  12. Nntp-Posting-Host: bart.crd.ge.com
  13.  
  14. In article <C04tnH.LCH@world.std.com>, dirsolns@world.std.com (Bernard Farrell) writes:
  15. |> darcy@druid.uucp (D'Arcy J.M. Cain) writes:
  16. |> : dirsolns@world.std.com (Bernard Farrell) writes:
  17. |> : >ah017@yfn.ysu.edu (John B. Lee) writes:
  18. |> : >: Is it possible to create an array that would change size according
  19. |> : >: to user input?
  20. |> : >float fl_arr[] = NULL;   /*  This is actually a float * -- no space  */
  21. |> : 
  22. |> : You didn't even try this did you?  This actually does assign space to an
  23. |> : array.  I don't know what your compiler does with the "= NULL" but assuming
  24. |> : it even accepts it (mine doesn't) it certainly doesn't do what you think it
  25. |> : does.  If you want a float * why didn't you just declare that:
  26. |> : 
  27. |> : float *fl_arr = NULL;
  28. |> : 
  29. |> 
  30. |> D'Arcy,
  31. |> 
  32. |> First of all, the approach I've outlined works for me on a number
  33. |> of machines.  Second, ff I'm going to treat the pointer as an array,
  34. |> I'd rather make this OBVIOUS in the declaration, even if the variable
  35. |> name does help.
  36.  
  37. Bernard,
  38.   I hate to tell you this, but D'Arcy is absolutely right and you are
  39. dead wrong. Pointers are not arrays, no matter how much they look alike
  40. in usage. Incorrect declarations will get you into trouble. The approach you
  41. outlined shows a misunderstanding of the differences between pointers and
  42. arrays. I will reiterate the faulty declaration above and explain what it
  43. actually means and what is actually being done (most likely) on YOUR machine:
  44.  
  45.     float fl_arr[] = NULL;   /*  This is actually a float * -- no space  */
  46.  
  47. The reason your compiler doesn't complain is that on your machine, NULL is
  48. defined as "0" (a perfectly legitimate definition) and thus the declaration
  49. becomes "float fl_arr[] = 0;". Technically, "0" is an invalid initializer
  50. list for an array, but your compiler seems to have an extension that allows
  51. you to omit braces for an aggragate initializer when there is only one
  52. element in the initializer list. So, the declaration is effectively
  53. "float fl_arr[] = {0};", which initializes an array of one element to 
  54. contain the value 0.0. Note that the length of the initializer list implicitly
  55. determines the size of the array, even though the size was omitted between
  56. the brackets. It's the same as saying "float fl_arr[1] = {0};".
  57.  
  58. The resulting array CANNOT be dynamically resized. The identifier
  59. "fl_arr" cannot be used on the left side of an assignment, as
  60. in "fl_arr = malloc(n * sizeof(float));" This is a constraint violation,
  61. and a conforming implementation is required to diagnose it. 
  62.  
  63. The reason D'Arcy's compiler would not accept the initial declaration
  64. is probably twofold: One, his NULL may be defined as "(void *) 0", a
  65. perfectly good definition of NULL, but not a valid initializer for 
  66. a float. Two, his compiler may very well reject an array initialization
  67. (except for char arrays being initialized with string literals, of course)
  68. in which braces are omitted. 
  69.  
  70. |> 
  71. |> I'd be interested to know the kind of machine you compiled this
  72. |> on, the ones I've done this before on may not be very ANSI compliant.
  73.  
  74. I probably missed the rest of your outlined method, but judging by your
  75. comment to the right of the declaration, I would imagine it's quite
  76. illegal.
  77.  
  78. -Chris
  79.  
  80. -- 
  81. ==================
  82. Chris Volpe
  83. G.E. Corporate R&D
  84. volpecr@crd.ge.com
  85.