home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / modula2 / 1751 < prev    next >
Encoding:
Internet Message Format  |  1993-01-28  |  2.5 KB

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!spool.mu.edu!samsung!balrog!ctron.com
  2. From: smith@ctron.com (Lawrence C Smith)
  3. Newsgroups: comp.lang.modula2
  4. Subject: Re: c and m2
  5. Keywords: c, m2
  6. Message-ID: <6568@balrog.ctron.com>
  7. Date: 26 Jan 93 18:12:45 GMT
  8. References: <1993Jan25.184552.23393@mcshub.dcss.mcmaster.ca>
  9. Sender: usenet@balrog.ctron.com
  10. Reply-To: smith@ctron.com
  11. Organization: Cabletron Systems, Inc.
  12. Lines: 42
  13. Nntp-Posting-Host: glinda
  14.  
  15. In article <1993Jan25.184552.23393@mcshub.dcss.mcmaster.ca>, cs4gp6au@maccs.mcmaster.ca (Lockie      RJ) writes:
  16. >/* p now points to test[1] according to K&R2 pg. 98 */
  17. >  (* p now points to test[0][1] *)
  18.  
  19. >So, if the above code is correct, the question I have is why does
  20. >incrementing a pointer in C point to the next element in the array?
  21. >while in Modula-2 it doesn't (the pointer needs to be incremented
  22. >by the size of the array element).
  23.  
  24. >How does C do that? Is it the compiler? In Modula-2, to increment to
  25. >the next element in the array it would require:
  26.  
  27. >  INC( p, SIZE(ex) );
  28.  
  29. The main difference between B and C is the notion of types.  B just had
  30. words, C had ints, and chars, and so on.  One of the original reasons for
  31. this change was so that the compiler could recognize pointer to <type>
  32. and increment by the appropriate number of bytes to get to the next
  33. element.
  34.  
  35. The reason this is so is because arrays are second-class citizens in C,
  36. merely vectors of elements, not a real type, like structs are.  You can
  37. see this in every function call, where an array is always passed by address
  38. (even the name is merely a constant referring to the address of the first
  39. element) and a struct is actually copied onto the stack, like any other
  40. parameter.  Vectors were set up so the programmer could scan them by walking
  41. a pointer up and down - in fact, C is completely designed around this
  42. assembly-code concept.
  43.  
  44. Modula and Pascal promote arrays to first-class citizens, they are treated
  45. just like records and they define _subscripting_ as the access mechanism,
  46. _not_ pointer dereferencing.  When you inc or dec a pointer you are poten-
  47. tially breaking typechecking by potentially breaking range-checking.
  48.  
  49. Modula's INC builtin is defined to increment its argument by one, no more.
  50. It is _not_ defined as a means of stepping to the next "reasonable" value.
  51. It is _not_ a pointer operation.
  52.  
  53. Larry Smith (smith@ctron.com)  No, I don't speak for Cabletron.  Need you ask?
  54. -
  55. Liberty is not the freedom to do whatever we want,
  56. it is the freedom to do whatever we are able.
  57.