home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / modula2 / 1739 < prev    next >
Encoding:
Text File  |  1993-01-25  |  1.3 KB  |  46 lines

  1. Newsgroups: comp.lang.modula2
  2. Path: sparky!uunet!utcsri!torn!mcshub!maccs!cs4gp6au
  3. From: cs4gp6au@maccs.mcmaster.ca (Lockie      RJ)
  4. Subject: c and m2
  5. Message-ID: <1993Jan25.184552.23393@mcshub.dcss.mcmaster.ca>
  6. Keywords: c, m2 
  7. Sender: cs4gp6au@maccs.dcss.mcmaster.ca
  8. Nntp-Posting-Host: maccs.dcss.mcmaster.ca
  9. Organization: Department of Computer Science, McMaster University
  10. Date: Mon, 25 Jan 1993 18:45:52 GMT
  11. Lines: 33
  12.  
  13.     Ok. I have a question about the difference between C and
  14. Modula-2.
  15. Here's some C code (correct I hope):
  16. struct ex {char word[10};
  17. struct ex test[10];
  18. struct ex *p;
  19. p = &test[0];   /* or p = test */
  20. p ++;
  21. /* p now points to test[1] according to K&R2 pg. 98 */
  22.  
  23. Here's some Modula-2 code:
  24. TYPE ex = ARRAY[0..10] OF CHAR;
  25. VAR test : ARRAY[0..10] OF ex;
  26.     p : POINTER TO ex;
  27. BEGIN
  28.   p = ADR(test[0]);
  29.   INC( p );
  30.   (* p now points to test[0][1] *)
  31.  
  32. So, if the above code is correct, the question I have is why does
  33. incrementing a pointer in C point to the next element in the array?
  34. while in Modula-2 it doesn't (the pointer needs to be incremented
  35. by the size of the array element).
  36. How does C do that? Is it the compiler? In Modula-2, to increment to
  37. the next element in the array it would require:
  38.   INC( p, SIZE(ex) );
  39.  
  40. So is this code correct? And how is this feature of C implemented?
  41.  
  42.     Bob Lockie: cs4gp6au@maccs.dcss.mcmaster.ca
  43.  
  44.  
  45.  
  46.