home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!hubcap!ncrcae!ncrhub2!ncrgw2!psinntp!heimdall!thor!scjones
- From: scjones@thor.sdrc.com (Larry Jones)
- Newsgroups: comp.lang.c
- Subject: Re: c and modula2
- Message-ID: <567@heimdall.sdrc.com>
- Date: 26 Jan 93 16:33:36 GMT
- References: <1993Jan25.184916.23553@mcshub.dcss.mcmaster.ca>
- Sender: news@heimdall.sdrc.com
- Lines: 39
-
- In article <1993Jan25.184916.23553@mcshub.dcss.mcmaster.ca>, cs4gp6au@maccs.mcmaster.ca (Lockie RJ) writes:
- > 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] *)
-
- A better translation of this to C than what you gave would be:
-
- typedef char ex[11]; /* [0..10] is 11 elements */
- ex test[11];
- ex *p;
- p = &test[0]; /* or p = test */
- p ++;
- /* p now points to test[1] according to K&R2 pg. 98 */
-
- > 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).
-
- Because that's the definition of pointer arithmetic in C. If p points
- to an 11 element array and you increment it, doesn't it make sense that
- it then points to the next 11 element array?
-
- > How does C do that? Is it the compiler?
-
- Yes, the compiler does whatever is necessary to meet the definition of
- pointer arithmetic. For an implementation on a traditional linear
- address space machine, this corresponds to multiplying the increment
- by the size of what the pointer points to and then adding the result
- to the address the pointer is currently pointing to.
- ----
- Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH 45150-2789 513-576-2070
- larry.jones@sdrc.com
- It's going to be a long year. -- Calvin
-