home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / std / c / 3439 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.7 KB  |  48 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!sdd.hp.com!usc!howland.reston.ans.net!spool.mu.edu!sgiblab!munnari.oz.au!yarrina.connect.com.au!warrane.connect.com.au!g2syd!andrewc
  3. From: andrewc@g2syd.genasys.com.au (Andrew Congdon)
  4. Subject: const qualifier
  5. Message-ID: <1993Jan28.073947.6951@g2syd.genasys.com.au>
  6. Organization: Genasys II, Sydney, Australia
  7. Date: Thu, 28 Jan 1993 07:39:47 GMT
  8. Lines: 38
  9.  
  10. I am still really confused about the use of the const qualifier.  I'd be
  11. grateful if someone could point me at a clear demonstration of its use.
  12.  
  13. I have a function which takes a parameter that is a 2 dimensional array
  14. which it will never modify.  A similar function takes the same parameter
  15. type but does modify its contents.  To make it clear I thought I'd make
  16. the former have a const qualified argument.  Except for a recently
  17. patched IBM ANSI C compiler, all the ANSI C compilers here complain
  18. about the following:
  19.  
  20.     extern void
  21.     fn(const float colours[][3]);
  22.  
  23.     static float colours[][3] = { { 1.0, 0.0, 0.0 } };
  24.  
  25.     fn2(void)
  26.     {
  27.         fn(colours);
  28.     }
  29.  
  30. The complaint is the parameter in the call to fn() does not match the
  31. prototype.  The compilers seem much happier if the call is changed to:
  32.  
  33.         fn((const float (*)[3])colours);
  34.  
  35. Why should the cast be necessary?
  36.  
  37. The DEC complaint is at least amusing:
  38.  
  39. const.c:9:  error:  In this statement, the referenced type of the
  40. pointer value "colours" is "array [3] of float", which is not compatible
  41. with "array [3] of float".
  42.  
  43. --
  44. Andrew Congdon    |  Genasys II Pty Ltd
  45.         |  13th Level, 33 Berry St, North Sydney, NSW, Australia
  46.         |  Phone:    +61-2-954-0022 (-9930 FAX)
  47.         |  Internet: andrewc@g2syd.genasys.com.au
  48.