1) Pointers 2) Type declarations 3) Access/reference values 4) Type renaming/aliasing 5) Subtypes 6) Macro substitution COMP205 IMPERATIVE LANGUAGES 4.

Slides:



Advertisements
Similar presentations
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Names - Design issues: - Maximum length? - Are connector characters allowed? - Are names case.
Advertisements

1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Programming Languages and Paradigms
Names and Bindings.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
1) Constrained (static) and unconstrained (dynamic) arrays. 2) Flexible arrays 3) Multi-dimensional arrays 4) Arrays of arrays 5) Lists, sets, bags,Etc.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Lecture 2 Introduction to C Programming
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
COMP205 IMPERATIVE LANGUAGES
Chapter 10.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
1) Fixed count loops 2) Variable count loops 3) Post-test Loops 4) Terminating a loop COMP205 IMPERATIVE LANGUAGES 11. PROGRAM CONSTRUCTS 2 (REPETITION)
COMP205 Comparative Programming Languages Part 1: Introduction to programming languages Lecture 3: Managing and reducing complexity, program processing.
1 ) Data Attributes 2) Declarations, Assignment and Instantiation 3) Global and Local Data 4) Variables 5) Constants 6) Example programs COMP205 IMPERATIVE.
1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header.
1) Data Review 2) Anonymous Data Items 3) Renaming 4) Overloading 5) Introduction to Data Types 6) Basic types 7) Range and precision 8) Ada/Pascal “attributes”
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
TYPE EQUIVALENCE 1) Coercion 2) Casting 3) Conversion.
1) Linked records/structures 2) Linked lists, examples in Ada and C 3) Processing linked lists COMP205 IMPERATIVE LANGUAGES 8. COMPOUND (HIGHER LEVEL)
Elementary Data Types Scalar Data Types Numerical Data Types Other
PZ04A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04A - Scalar and composite data Programming Language.
1) Introduction to higher level types 2) Arrays and their declaration 3) Assigning values to array elements 4) Operations on arrays 5) Ada array attributes.
String Escape Sequences
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Programming Languages
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Names Variables Type Checking Strong Typing Type Compatibility 1.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
CPS120: Introduction to Computer Science
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Learners Support Publications Classes and Objects.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Variables reference, coding, visibility. Rules for making names  permitted character set  maximum length, significant length  case sensitivity  special.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
CPS120: Introduction to Computer Science Variables and Constants.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Data Types In Text: Chapter 6.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
COMP205 IMPERATIVE LANGUAGES
COMP205 IMPERATIVE LANGUAGES
Revision Lecture
Names, Bindings, and Scopes
C Basics.
Introduction to Abstract Data Types
Support for Object-Oriented Programming in Ada 95
Classes and Objects.
High Level Programming Languages
The Data Element.
The Data Element.
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

1) Pointers 2) Type declarations 3) Access/reference values 4) Type renaming/aliasing 5) Subtypes 6) Macro substitution COMP205 IMPERATIVE LANGUAGES 4. MORE ON BASIC DATA TYPES

POINTERS Pointer variables have as their value an address, i.e. a reference to another data item. A pointer may take a special value null which indicates that the pointer is pointing nowhere. Given a pointer we can access the value "pointed" to through a process known as dereferencing.

C POINTERS C makes extensive use of pointers and uses two prefix operators: * (Asterisk) & (Ampersand) * when used in a declaration is read as "pointer to ”: * when used elsewhere is interpreted as "the value contained in the reference pointed at (i.e. the pointer is dereferenced)": & when used anywhere is read as the "reference/address of...": In this case x is referred to as a reference variable. *numberPtr = *numberPtr + 1; int *numberPtr; numberPtr = &x

#include void main(void) { int n, *nptr; n = 2; nptr = &n; printf("n = %d\n",n); printf("address of n = %d\n",&n); printf("nptr = %d\n",nptr); printf("address of nptr = %d\n",&nptr); printf("value pointed at = %d\n\n", *nptr); n = 4; printf("n = %d\n",n); printf("address of n = %d\n",&n); printf("nptr = %d\n",nptr); printf("address of nptr = %d\n",&nptr); printf("value pointed at = %d\n\n", *nptr); } n = 2 address of n = nptr = address of nptr = value pointed at = 2 n = 4 address of n = nptr = address of nptr = value pointed at = 4

ACCESS/REFERENCE VALUES Although language such as Ada and Pascal do not specifically support the concept of pointers as used in C, however it is possible to define a variable that has as it value an address. Such data items are known as access or reference variables (the concept also exists in Java). However, before we can explore the concept of access values we need to consider type declarations in imperative languages.

TYPE DECLARATIONS There are many situations where it is desirable for programmers to define their own types. 1) To enhance clarity and readability. 2) To obtain a better representation of reality. 3) To facilitate automatic checks on values. i.e. to produce more reliable programs. Not all languages support the concept of programmer defined types (Ada does, C does not). Where supported programmer-defined types are presented using a type declaration statement. This binds a "new" type definition to a type name (whereas a data declaration statement binds an existing type definition to a data item name).

TYPE DECLARATIONS IN ADA In Ada type declaration statements have the general form: Example type declarations incorporating specific ranges and/or precisions: Note that in each case the compiler will deduce that the types SCORE and TEMPERATURE are integer types. Similarly the compiler will deduce that the type PERCENTAGE is a floating point type. type is ; type SCORE is range ; type TEMPERATURE is digits 3; type PERCENTAGE is digits 4 range ;

TYPE DECLARATIONS IN ADA CONTINUED Once the above types have been declared they can be utilised in the usual manner: If, in the above declarations, an attempt is made to assign a value outside the specified range an error will result. A_VALUE: SCORE; SUMMER: TEMPERATURE := 20; X_VALU, Y_VALU: PERCENTAGE := 100.0;

BACK TO ACCESS/REFERENCE VALUES with CS_IO; use CS_IO; procedure EXAMPLE is type INTEGERPTR is access integer; INT_PTR: INTEGERPTR:= new integer'(2); begin put(INT_PTR.ALL); new_line; end EXAMPLE; To declare an access value we use a type declaration and include the reserved word access in the definition.

DISTINCTION BETWEEN POINTERS AND ACCESS VALUES Pointers are more versatile than reference values: 1) We can inspect the value (address) of a pointer. 2) We can perform arithmetic on pointer values. 3) We can look at “the value pointed at” (dereferencing). Access values only allow “access” to the data item referred to.

MORE ON TYPE DECLARATIONS We have noted that the use of user defined types offers advantages of. 1) To enhance clarity and readability. 2) To obtain a better representation of reality. 3) To facilitate automatic checks on values. i.e. to produce more reliable programs. These advantages can be improved upon through a number of other “typing” techniques: 1) Aliasing/renaming of predefined type names. 2) Creating sub-types of existing types 3) Using macro substitutions

ALIASING (RENAMING) TYPE NAMES It is sometimes desirable, given a particular application, to use more descriptive (application dependent) type names for particular types. This may offer further advantages of 1) Clarity, and 2) Brevity In Ada this is achieved (again) using a type declaration statement: Type REAL is float NUMBER1: REAL;

In C a type definition construct is used: In both examples an alternative name for the type "float" has been declared. In addition, in the C example, a pointer to the new type has also been declared. Note that by convention, in C, alternative names are typed in uppercase. typedef float REAL, *REALPTR; REAL x; REALPTR xPtr;

SUB-TYPES Given a particular application it is sometimes useful to characterise a sub-set of values of some other type. This is supported by languages such as Ada (but not C). In Ada this is achieved using a subtype declaration statement: subtype DAY_NUMBER is integer range 1..31; Y: DAY_NUMBER:= 10;

PREDEFINED SUB-TYPES Some imperative languages that support the concept of sub-types also provide some predefined (“built-in”) sub- types. For examples Ada supports two predefined sub-types of the type INTEGER which are defined as follows: (note the use of the integer attribute last). subtype NATURAL is INTEGER range 0..INTEGER'LAST; subtype POSITIVE is INTEGER range 1..INTEGER'LAST;

ADVANTAGES OF SUB-TYPES Use of subtypes offers similar advantages to those associated with the use of programmer defined types: 1) Readability. 2) Good representation of reality. 3) Error checking. WHEN TO USE TYPE AND SUBTYPE DECLARATIONS Both offer similar advantages (see above), however generally speaking, where a data item is required to display many of the features of the standard INTEGER or FLOAT types a subtype declaration should be used. Type declarations are usually used to describe compound types (e.g. arrays), but more on this later.

C MACRO SUBSTITUTION Macro substitution is where some string replaces every occurrence of an identifier in a program. This is supported by C (but not Ada and Pascal). To define a macro substitution we include a statement at the beginning of our program of the form: For example: The effect of this is that wherever the name TRUE appears in our program this will be replaced with the string 1 on compilation, and FALSE by 0. #define #define TRUE 1 #define FALSE 0

SUMMARY 1) Pointers 2) Type declarations 3) Access/reference values 4) Type renaming/aliasing 5) Subtypes 6) Macro substitution