COMP205 IMPERATIVE LANGUAGES

Slides:



Advertisements
Similar presentations
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.
Advertisements

Programming Languages and Paradigms
Names and Bindings.
1) Constrained (static) and unconstrained (dynamic) arrays. 2) Flexible arrays 3) Multi-dimensional arrays 4) Arrays of arrays 5) Lists, sets, bags,Etc.
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.
COMP205 IMPERATIVE LANGUAGES
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
1) Fixed count loops 2) Variable count loops 3) Post-test Loops 4) Terminating a loop COMP205 IMPERATIVE LANGUAGES 11. PROGRAM CONSTRUCTS 2 (REPETITION)
1) Pointers 2) Type declarations 3) Access/reference values 4) Type renaming/aliasing 5) Subtypes 6) Macro substitution COMP205 IMPERATIVE LANGUAGES 4.
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”
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)
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
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
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Expressions and Assignment Statements
Pointers and Dynamic Arrays
Data Types In Text: Chapter 6.
Chapter 6 JavaScript: Introduction to Scripting
Introduction to Linked Lists
Objectives In this chapter, you will:
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Expressions and Assignment Statements
COMP205 IMPERATIVE LANGUAGES
Pointers, Enum, and Structures
Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.
Principles of programming languages 4: Parameter passing, Scope rules
Revision Lecture
Names, Bindings, and Scopes
Expressions and Assignment Statements
C Basics.
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Chapter 8: Control Structures
Introduction to Linked Lists
Pointers and Arrays Chapter 12
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to Abstract Data Types
Expressions and Assignment Statements
Support for Object-Oriented Programming in Ada 95
Programming in C Pointer Basics.
Classes and Objects.
C Storage classes.
Programming in C Pointer Basics.
Regrading Project 2 Exam 2 One week period TA: Huiyuan TA: Fardad
Pointers and Arrays Chapter 12
PZ04A - Scalar and composite data
Overloading functions
POINTER CONCEPT 4/15/2019.
Java Programming Language
Names, Bindings, and Scopes
DATA TYPES There are four basic data types associated with variables:
POINTER CONCEPT 8/3/2019.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

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

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 <DATATYPE>”: * 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. int *numberPtr; *numberPtr = *numberPtr + 1; numberPtr = &x

#include <stdio.h> 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; } n = 2 address of n = 2063808352 nptr = 2063808352 address of nptr = 2063808356 value pointed at = 2 n = 4 address of n = 2063808352 nptr = 2063808352 address of nptr = 2063808356 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 <TYPE_NAME> is <TYPE DEFINITION>; type SCORE is range 0 .. 100; type TEMPERATURE is digits 3; type PERCENTAGE is digits 4 range 0.0 .. 100.0;

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 WHEN TO USE TYPE AND SUBTYPE DECLARATIONS 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 <macro name> <macro string> #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