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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!haven.umd.edu!darwin.sura.net!paladin.american.edu!news.univie.ac.at!hp4at!mcsun!sunic!kth.se!lysator.liu.se!ronnie
  3. From: ronnie@lysator.liu.se (Ronnie Sahlberg)
  4. Subject: Re: What is meant by: double A[n,m]?
  5. Message-ID: <1986@lysator.liu.se>
  6. Sender: news@lysator.liu.se
  7. Nntp-Posting-Host: robert.lysator.liu.se
  8. Organization: Lysator Academic Computer Society, Linkoping University, Sweden
  9. References: <TORSTEIN.92Nov18125146@itekiris.kjemi.unit.no>
  10. Date: Wed, 18 Nov 1992 21:37:17 GMT
  11. Lines: 64
  12.  
  13. torstein@itekiris.kjemi.unit.no (torstein hansen) writes:
  14.  
  15. >As a newcomer to C, previously programming in Pascal, I used the
  16. >following declaration in one of my programs:
  17.  
  18. >double A[n,m];
  19.  
  20. >I assumed (erronously) that this would give a n by m matrix. But
  21. >obviously, it didn't.  But the compiler never gave any errors or
  22. >warnings, and using 
  23.  
  24. The compiler didnt flag any errors since there is nothing syntacticaly wrong
  25. with the statement.
  26. The comma-operator is used to string several expressions together into one 
  27. expression. The comma-operator has a defined order of evaluation (as && and || also have) from left to right and the value of the resulting expression is the
  28. right operand of the comma-operator.
  29. Thus when writing   A[n,m]  it will perform like :
  30. 1,  Evaluate n and throw this value away.
  31. 2,  Evaluate m and use this value to index array A.
  32.  
  33. >  A[rownumber,colnumber]=some_value;
  34. >was also accepted by the compiler.  I'm not asking for the correct 
  35. >way to define an array, I found that out quickly, but what is meant 
  36. >by A[n,m]?  It seems like the compiler just ignores the n and makes 
  37. >it A[m].  This is also the case when assigning values to the array.
  38.  
  39. when writing double A[n,m] you get access to the same element as if you had 
  40. written double A[m].
  41. You can even write something like A[printf("Hello World\n"),m]; and it will
  42.  also be a valid C expression, although it might look quite weird.
  43.  
  44. >That is:
  45. >  double A[2,2];  
  46. >gave same result as 
  47. >  double A[2];
  48.  
  49. >  A[0,1] = some_value;
  50. >gave same result as
  51. >  A[1,1] = some_value;
  52. >and 
  53. >  A[1] = some_value;
  54.  
  55. >Can somebody enlighten me in this matter?  I couldn't find anything
  56. >about it in any of the C books I have.  If double A[n,m] is illegal,
  57. >shouldn't I get a warning at least?
  58.  
  59. >Has it something to do with the comma operator, discarding the first
  60. >value inside the braces?
  61.  
  62. >By the way, the compiler I used was Turbo C++ ver. 3.1, just in case
  63. >this behaviour isn't standard.
  64.  
  65. It is standard behaviour.
  66.  
  67. >Torstein Hansen
  68. >torstein@itekiris.kjemi.unit.no
  69. >--
  70. >Torstein Hansen
  71. >torstein@itekiris.kjemi.unit.no
  72.  
  73.  
  74.  
  75. ronnie sahlberg      ronnie@lysator.liu.se
  76.