Discussion:
Pointer Assignment Compatability
(too old to reply)
Michael Daniluk
2009-04-21 22:18:33 UTC
Permalink
Hello All,

Very New to Modula 2 and I am getting a compiler error while trying
to assign one pointer to another. The purpose of this code is to
implement my own little "dynamic array" abstract data type. Here is a
snippet of the Implementation Module:

TYPE String = POINTER TO CharArr;

(*This Record Defines The String Type*)
TYPE CharArr = RECORD;
value : POINTER TO CHAR;
size : INTEGER;
capacity : INTEGER;
END;


PROCEDURE ReallocArr (VAR s : String; newCapacity : INTEGER);

VAR OldPointer, NewPointer, run_1, run_2 : POINTER TO CHAR;
VAR counter : INTEGER;

BEGIN
OldPointer := s^.value; (*ERROR OCCURS HERE*)


As indicated, my error occurs in the last line of code I posted. The
error is "expression is not assignment compatible." It is my
understanding that both 'OldPointer' and 's^.value' are of the type
POINTER TO CHAR, and therefore the assignment operation should work.
What insight is eluding me here? Thanks in Advance.

truly,
Michael
Marco van de Voort
2009-04-22 12:04:13 UTC
Permalink
Post by Michael Daniluk
As indicated, my error occurs in the last line of code I posted. The
error is "expression is not assignment compatible." It is my
understanding that both 'OldPointer' and 's^.value' are of the type
POINTER TO CHAR, and therefore the assignment operation should work.
What insight is eluding me here? Thanks in Advance.
Probably two different uses of "POINTER TO CHAR" create two different (and
thus incompatible) types.

Define

TYPE
PCHAR = POINTER TO CHAR;

and then use PCHAR everywhere.
Michael Daniluk
2009-04-22 14:11:15 UTC
Permalink
Post by Marco van de Voort
Post by Michael Daniluk
As indicated, my error occurs in the last line of code I posted. The
error is "expression is not assignment compatible." It is my
understanding that both 'OldPointer' and 's^.value' are of the type
POINTER TO CHAR, and therefore the assignment operation should work.
What insight is eluding me here? Thanks in Advance.
Probably two different uses of "POINTER TO CHAR" create two different (and
thus incompatible) types.
Define
TYPE
PCHAR = POINTER TO CHAR;
and then use PCHAR everywhere.
Thanks, that worked. But now the important question: Why? It seems that
a type definition is something other than just a literal translation.
Anyone have thoughts on this?
Chris Burrows
2009-04-23 00:14:16 UTC
Permalink
Thanks, that worked. But now the important question: Why? It seems that a
type definition is something other than just a literal translation. Anyone
have thoughts on this?
What Modula-2 books / documentation are you currently referring to in your
efforts to learn the language? If you need some good references let us know.

Modula-2 uses 'name compatibility' rules rather than 'structure
compatibility' rules when determining if two variables are
assignment-compatible.
In your declaration:

e : POINTER TO CHAR;

'POINTER TO CHAR' is an 'anonymous type' defined 'on the fly' in a VAR
declaration rather than a 'named type' defined in a TYPE declaration.

Some more obvious examples may help you to understand the difference between
'name compatibility' and 'structure compatibility':

e.g. given the following declarations:

TYPE
Dimensions = ARRAY [0..9] OF INTEGER;
Sizes = Dimensions;

VAR
lengths, widths: Dimensions;
heights: Sizes;
weights1, weights2: ARRAY [0..9] OF INTEGER;
kgs: ARRAY [0..9] OF INTEGER;

(* The following assignments are compatible *)

lengths := widths;
heights := widths;
weights1 := weights2;

(* The following assignments are incompatible *)

lengths := weights1;
weights2 := heights;
kgs := weights1;

--
Chris Burrows
CFB Software
http://www.cfbsoftware.com/modula2
Michael Daniluk
2009-04-23 15:21:34 UTC
Permalink
Thank You Chris,

That certainly sheds light on the issue. I am currently reading
"Modula 2 Programming 2nd Edition" by BJ Holmes. I find it lacking. It
seems like a freshman book geared towards readers with no programming
experience. Right now I am looking for the system call to exit a
program, and sure enough the book does not contain any reference to one.
Any suggestion for better reference would be very cool :)

truly,
Michael
Post by Chris Burrows
Thanks, that worked. But now the important question: Why? It seems that a
type definition is something other than just a literal translation. Anyone
have thoughts on this?
What Modula-2 books / documentation are you currently referring to in your
efforts to learn the language? If you need some good references let us know.
Modula-2 uses 'name compatibility' rules rather than 'structure
compatibility' rules when determining if two variables are
assignment-compatible.
e : POINTER TO CHAR;
'POINTER TO CHAR' is an 'anonymous type' defined 'on the fly' in a VAR
declaration rather than a 'named type' defined in a TYPE declaration.
Some more obvious examples may help you to understand the difference between
TYPE
Dimensions = ARRAY [0..9] OF INTEGER;
Sizes = Dimensions;
VAR
lengths, widths: Dimensions;
heights: Sizes;
weights1, weights2: ARRAY [0..9] OF INTEGER;
kgs: ARRAY [0..9] OF INTEGER;
(* The following assignments are compatible *)
lengths := widths;
heights := widths;
weights1 := weights2;
(* The following assignments are incompatible *)
lengths := weights1;
weights2 := heights;
kgs := weights1;
--
Chris Burrows
CFB Software
http://www.cfbsoftware.com/modula2
Chris Burrows
2009-04-24 01:30:27 UTC
Permalink
Post by Michael Daniluk
Thank You Chris,
That certainly sheds light on the issue. I am currently reading
"Modula 2 Programming 2nd Edition" by BJ Holmes. I find it lacking. It
seems like a freshman book geared towards readers with no programming
experience. Right now I am looking for the system call to exit a program,
and sure enough the book does not contain any reference to one. Any
suggestion for better reference would be very cool :)
Is HALT what you are looking for?

A copy of Niklaus Wirth's "Programming in Modula-2" (often referred to as
PIM) is essential. There are four editions reflecting the evolution of the
language. Look for the edition that best corresponds to the implementation
that you are using. A summary of the differences is in the 'Dialects'
section of the Modula-2 Wikipedia page:

http://en.wikipedia.org/wiki/Modula-2

Also I would suggest: "Modula-2 - A Complete Guide" by K N King:

http://knking.com/books/modula2/

I don't actually have this book myself. However, I consider the Language
Tutorial included with JPI Modula-2 to be well-written. It is a condensed
(from 650 down to 190 pages) version of this book.

For a good selection of general purpose algorithms written in Modula-2, you
can't go past "Algorithms and Data Structures" by Niklaus Wirth:

http://www.inf.ethz.ch/personal/wirth/books/AlgorithmE1/

Make sure you get the 1986 version. The earlier (1976) version "Algorithms +
Data Structures = Programs" used Pascal. If you want to get an idea what it
is like, there is a later (2004) Oberon PDF version that you can download
from:

http://www-old.oberon.ethz.ch/WirthPubl/AD.pdf

They should keep you busy for a while ...

--
Chris Burrows
CFB Software
http://www.cfbsoftware.com/modula2

Michael Daniluk
2009-04-23 15:23:17 UTC
Permalink
Thank You Chris,

That certainly sheds light on the issue. I am currently reading
"Modula 2 Programming 2nd Edition" by BJ Holmes. I find it lacking. It
seems like a freshman book geared towards readers with no programming
experience. Right now I am looking for the system call to exit a
program, and sure enough the book does not contain any reference to one.
Any suggestion for better reference would be very cool :)

truly,
Michael
Post by Chris Burrows
Thanks, that worked. But now the important question: Why? It seems that a
type definition is something other than just a literal translation. Anyone
have thoughts on this?
What Modula-2 books / documentation are you currently referring to in your
efforts to learn the language? If you need some good references let us know.
Modula-2 uses 'name compatibility' rules rather than 'structure
compatibility' rules when determining if two variables are
assignment-compatible.
e : POINTER TO CHAR;
'POINTER TO CHAR' is an 'anonymous type' defined 'on the fly' in a VAR
declaration rather than a 'named type' defined in a TYPE declaration.
Some more obvious examples may help you to understand the difference between
TYPE
Dimensions = ARRAY [0..9] OF INTEGER;
Sizes = Dimensions;
VAR
lengths, widths: Dimensions;
heights: Sizes;
weights1, weights2: ARRAY [0..9] OF INTEGER;
kgs: ARRAY [0..9] OF INTEGER;
(* The following assignments are compatible *)
lengths := widths;
heights := widths;
weights1 := weights2;
(* The following assignments are incompatible *)
lengths := weights1;
weights2 := heights;
kgs := weights1;
--
Chris Burrows
CFB Software
http://www.cfbsoftware.com/modula2
Loading...