🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #2433

Re: c and m2

#2433
From: Lawrence C Smith
Date: Tue, 26 Jan 1993 18:12
42 lines
2597 bytes
In article <1993Jan25.184552.23393@mcshub.dcss.mcmaster.ca>, cs4gp6au@maccs.mcmaster.ca (Lockie      RJ) writes:
>/* p now points to test[1] according to K&R2 pg. 98 */
>  (* 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) );

The main difference between B and C is the notion of types.  B just had
words, C had ints, and chars, and so on.  One of the original reasons for
this change was so that the compiler could recognize pointer to <type>
and increment by the appropriate number of bytes to get to the next
element.

The reason this is so is because arrays are second-class citizens in C,
merely vectors of elements, not a real type, like structs are.  You can
see this in every function call, where an array is always passed by address
(even the name is merely a constant referring to the address of the first
element) and a struct is actually copied onto the stack, like any other
parameter.  Vectors were set up so the programmer could scan them by walking
a pointer up and down - in fact, C is completely designed around this
assembly-code concept.

Modula and Pascal promote arrays to first-class citizens, they are treated
just like records and they define _subscripting_ as the access mechanism,
_not_ pointer dereferencing.  When you inc or dec a pointer you are poten-
tially breaking typechecking by potentially breaking range-checking.

Modula's INC builtin is defined to increment its argument by one, no more.
It is _not_ defined as a means of stepping to the next "reasonable" value.
It is _not_ a pointer operation.

Larry Smith (smith@ctron.com)  No, I don't speak for Cabletron.  Need you ask?
-
Liberty is not the freedom to do whatever we want,
it is the freedom to do whatever we are able.
Technical Details
Message-ID: <6568@balrog.ctron.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!spool.mu.edu!samsung!balrog!ctron.com
References: <1993Jan25.184552.23393@mcshub.dcss.mcmaster.ca>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!spool.mu.edu!samsung!balrog!ctron.com
From: smith@ctron.com (Lawrence C Smith)
Newsgroups: comp.lang.modula2
Subject: Re: c and m2
Keywords: c, m2
Message-ID: <6568@balrog.ctron.com>
Date: 26 Jan 93 18:12:45 GMT
References: <1993Jan25.184552.23393@mcshub.dcss.mcmaster.ca>
Sender: usenet@balrog.ctron.com
Reply-To: smith@ctron.com
Organization: Cabletron Systems, Inc.
Lines: 42
Nntp-Posting-Host: glinda