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

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