Chandra,

>I have 2 small pieces of code:
>
>Code - 1
>--------
>type
>  TDataType = Array[0..3] of char;
>
>procedure hello;
>var
>  a,b : Array[0..3] of char;
>  c,d : TDataType;
>begin
>  a := b;
>  c := d;
>end;
>
>
>Code - 2
>--------
>type
>  TDataType = Array[0..3] of char;
>
>procedure hello;
>var
>  a : Array[0..3] of char;
>  b : Array[0..3] of char;
>  c : TDataType;
>  d : TDataType;
>begin
>  a := b;
>  c := d;
>end;
>
>-------------
>Code1 gets compiled and runs fine but code2 gives compile time error 
>in line a := b;

I assume the error is an incompatible type error.
Pascal implements "strict typing". This means that
the variable types must exactly the same for the
compiler to allow assignments. I am guessing that
you assume the two separately declared unnamed types
Array[0..3] of char; are exactly the same. For scalar
variables, this is true. Separately declared integers, for
example are always assignment compatible. Structures,
on the other hand, like arrays, are not assumed to be
identical just because they have the same definition.
For example, let's say you have these structures:

var
  EmployeeID: array [0..20] of char;
  EmployeeAddress: array [0..20] of char;

Even though these two types happen to be the same
length, a Pascal compiler should enforce strict typing
and prevent you from inadvertently assigning an EmployeeID
to an EmployeeAddress. Obviously, such an assignment
would not make sense. The reason you first example worked
for a:=b is because you defined them in the same var
declaration, so the compiler could assume they were exactly
the same type. The reason c:=d works in both examples
is because you used a named type. When a named type
is used, the compiler can assume the types are identical.

Hope this helps,

Glenn Lawler
www.incodesystems.com

Reply via email to