Data Types: ISBN 0-321-49362-1
Data Types: ISBN 0-321-49362-1
Data Types
ISBN 0-321—49362-1
Chapter 6 Topics
• Introduction
• Primitive Data Types
• Character String Types
• User-Defined Ordinal Types
• Array Types
• Associative Arrays
• Record Types
• Union Types
• Pointer and Reference Types
• Simplest of all
• Range of values: two elements, one for
―true‖ and one for ―false‖
• Could be implemented as bits, but often as
bytes
– Advantage: readability
• Typical operations:
– Assignment and copying
– Comparison (=, >, etc.)
– Catenation
– Substring reference
– Pattern matching
• Aid to writability
• As a primitive type with static length, they
are inexpensive to provide--why not have
them?
• Dynamic length is nice, but is it worth the
expense?
Compile-time Run-time
descriptor for descriptor for
static strings limited dynamic
strings
Copyright © 2009 Addison-Wesley. All rights reserved. 1-17
User-Defined Ordinal Types
Day1: Days;
Day2: Weekday;
Day2 := Day1;
• Aid to readability
– Make it clear to the readers that variables of
subrange can store only certain range of values
• Reliability
– Assigning a value to a subrange variable that is
outside the specified range is detected as an
error
• C-based languages
– int list [] = {1, 3, 5, 7}
– char *names [] = {―Mike‖, ―Fred‖,―Mary Lou‖};
• Ada
– List : array (1..5) of Integer :=
(1 => 17, 3 => 34, others => 0);
• Python
– List comprehensions
list = [x ** 2 for x in range(12) if x % 3 == 0]
puts [0, 9, 36, 81] in list
• Fortran 95
Integer, Dimension (10) :: Vector
Integer, Dimension (3, 3) :: Mat
Integer, Dimension (3, 3) :: Cube
float stuff[100];
float *p;
p = stuff;
Language examples:
– FORTRAN 95 is not: parameters, EQUIVALENCE
– C and C++ are not: parameter type checking
can be avoided; unions are not type checked
– Ada is, almost (UNCHECKED CONVERSION is
loophole)
(Java and C# are similar to Ada)