Article View: comp.lang.modula2
Article #2421c and m2
From: Lockie RJ
Date: Mon, 25 Jan 1993 18:45
Date: Mon, 25 Jan 1993 18:45
32 lines
1407 bytes
1407 bytes
Ok. I have a question about the difference between C and
Modula-2.
Here's some C code (correct I hope):
struct ex {char word[10};
struct ex test[10];
struct ex *p;
p = &test[0]; /* or p = test */
p ++;
/* p now points to test[1] according to K&R2 pg. 98 */
Here's some Modula-2 code:
TYPE ex = ARRAY[0..10] OF CHAR;
VAR test : ARRAY[0..10] OF ex;
p : POINTER TO ex;
BEGIN
p = ADR(test[0]);
INC( p );
(* p now points to test[0][1] *)
So, if the above code is correct, the question I have is why does
incrementing a pointer in C point to the next element in the array?
while in Modula-2 it doesn't (the pointer needs to be incremented
by the size of the array element).
How does C do that? Is it the compiler? In Modula-2, to increment to
the next element in the array it would require:
INC( p, SIZE(ex) );
So is this code correct? And how is this feature of C implemented?
Bob Lockie: cs4gp6au@maccs.dcss.mcmaster.ca
Technical Details
Message-ID:
<1993Jan25.184552.23393@mcshub.dcss.mcmaster.ca>Path:
arc1-ca.pugleaf.net!sparky!uunet!utcsri!torn!mcshub!maccs!cs4gp6auShow Full Headers
Complete Article Headers
Newsgroups: comp.lang.modula2 Path: arc1-ca.pugleaf.net!sparky!uunet!utcsri!torn!mcshub!maccs!cs4gp6au From: cs4gp6au@maccs.mcmaster.ca (Lockie RJ) Subject: c and m2 Message-ID: <1993Jan25.184552.23393@mcshub.dcss.mcmaster.ca> Keywords: c, m2 Sender: cs4gp6au@maccs.dcss.mcmaster.ca Nntp-Posting-Host: maccs.dcss.mcmaster.ca Organization: Department of Computer Science, McMaster University Date: Mon, 25 Jan 1993 18:45:52 GMT Lines: 33