home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16728 < prev    next >
Encoding:
Text File  |  1992-11-18  |  2.0 KB  |  58 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: What is meant by: double A[n,m]?
  5. Message-ID: <1992Nov18.172026.21375@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <TORSTEIN.92Nov18125146@itekiris.kjemi.unit.no>
  8. Date: Wed, 18 Nov 1992 17:20:26 GMT
  9. Lines: 47
  10.  
  11. torstein@itekiris.kjemi.unit.no (torstein hansen) writes:
  12.  
  13. >As a newcomer to C, previously programming in Pascal, I used the
  14. >following declaration in one of my programs:
  15.  
  16. >double A[n,m];
  17.  
  18. >I assumed (erronously) that this would give a n by m matrix. But
  19. >obviously, it didn't.  But the compiler never gave any errors or
  20. >warnings, and using 
  21.  
  22. >  A[rownumber,colnumber]=some_value;
  23. >was also accepted by the compiler.  I'm not asking for the correct 
  24. >way to define an array, I found that out quickly, but what is meant 
  25. >by A[n,m]?  It seems like the compiler just ignores the n and makes 
  26. >it A[m].  This is also the case when assigning values to the array.
  27.  
  28. Right.  The declaration
  29.     double A[n, m];
  30. is not strictly conforming C code, but many (most) compilers allow
  31. the comma-expression inside the brackets.  The "n" is ignored and
  32. the declaration is equivalent to
  33.     double A[m]
  34. as you surmised.
  35.  
  36. The expression (as opposed to declaration)
  37.     A[x, y]
  38. is perfectly legal, having a comma-expression inside the brackets.
  39. The 'x' on the left side of the comma is evaluated and the result is
  40. ignored.  Except for side effects of evaluating 'x', the expression
  41. is equivalent to
  42.     A[y]
  43.  
  44. The difference is that a declaration of an array requires a constant-
  45. expression for the size, and a constant-expression may not contain a
  46. comma-expression.  Many compilers allow it anyway, as long as the left
  47. side of the comma is a constant-expression.
  48.  
  49. The C Standard is not absolutely clear on this point for declarations.
  50. In one place it forbids a comma-expression, and in another it says it
  51. is an allowable extension.
  52.  
  53. -- 
  54.  
  55. Steve Clamage, TauMetric Corp, steve@taumet.com
  56. Vice Chair, ANSI C++ Committee, X3J16
  57.