ch6 Short
ch6 Short
Data Types
Chapter 6 Topics
• Introduction
• Primitive Data Types
• Character String Types
• User-Defined Ordinal Types
• Array Types
• Associative Arrays
• Record Types
• Tuple Types
• List Types
• Union Types
• Pointer and Reference Types
• Type Checking
• Strong Typing
• Type Equivalence
• Theory and Data Types
Copyright © 2012 Addison-Wesley. All rights reserved. 1-2
Introduction
• 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
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
• Python
vector = [2, 4, 6, 8, 10, 12, 14, 16]
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
float stuff[100];
float *p;
p = stuff;
Language examples:
– 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)