This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Chapter 10 Structures and Derived Data Types Modern programming languages provide programmers with the mean to create and manipulate new data type, much like the intrinsic data types, like integer , real , and logical encountered so far. In C and C++, these are called structures and in fortran they are called derived data type . 10.1 Derived Data Types In many applications the intrinsic data types are not enough to express in code the ideas behind an algorithm or solution to a specific problem. Derived data types and structures allow programmer to group different kinds of information that belong to a single entity. In a way they resemble arrays but with two important differences. First the different elements of a derived data type do not have to be of the same type, thus they may include integer , character or real . Second the different entities making up the derived data type are referred to with a name and not an integer index. The different element of a derived data type are referred to as components . The data type of the component can be any of the intrinsic data types, or a previously defined derived data type. 10.2 Defining Derived Data Types A derived data type is defined with a block of code enclosed within a type and end type statements followed by the name we wish to give to that derived data type. For example suppose we are writing a program that manipulates geometric information, and we would like to define points and polygons. Furthermore, we would like a point to have some attributes associated with it. For example if solving a PDE, we would like to know the type of boundary condition applied at that node. 87 88 CHAPTER 10. STRUCTURES AND DERIVED DATA TYPES A point data type in 2D space can thus be defined: type :: point real :: x(2) ! coordinates of point in 2D space integer :: bccode ! boundary condition code end type point The components of point are a real vector x of dimension 2, and an integer bccode . Once a type has been declared it can be used to declare variables of that type. For example the following code declares two variables, A and B and C , of type point : type(point) :: A,B,C A = point( (/0.0,0.0/), 0) ! constructor for point A B = point( (/1.0,0.0/), 0) ! constructor for point B C = point( (/0.0,1.0/), 0) ! constructor for point C The statement A = point( (/0.0,0.0/), 0) is a default constructor for data type point . The constructor is essentially like a function call whose name is that of the derived data type, and whose argument consist of the data type component listed in the order of their declaration in the data type definition; in this case it is the vector of real followed by the integer. One can also define arrays of derived data type, for example type(point) :: circle(64) declares an 1D array of points of size 64....
View Full Document
- Fall '08