[PDF] [PDF] Ada-95: A guide for C and C++ programmers

Another useful reference is the Lovelace on-line tutorial which is a great way to pick up Ada basics I will start out by describing the Ada predefined types, and the  



Previous PDF Next PDF





[PDF] Introduction to Ada (PDF) - Learning Ada - AdaCore

26 fév 2021 · for proposals for a common, modern programming language This tutorial will focus on Ada 2012 as a whole, rather than teaching different 



[PDF] Ada–A Crash Course

1 Tutorial Welcome to the Ada programming language The purpose of this tutorial is to give you an overview of Ada so that you can start writing Ada programs 



[PDF] ADA PROGRAMMING - FreeInfoSocietycom

Programming in the large GNAT, the GNU Ada Compiler from AdaCore and the Free Software Welcome to the Ada Programming tutorial at Wikibooks



[PDF] Ada-95: A guide for C and C++ programmers

Another useful reference is the Lovelace on-line tutorial which is a great way to pick up Ada basics I will start out by describing the Ada predefined types, and the  



[PDF] Ada Reference Manual - Ada Resource Association

Ada Reference Manual - Language and Standard Libraries The International Standard for the programming language Ada is ISO/IEC 8652:1995(E) compiler writers, validation test writers, and others who wish to study the fine details The



[PDF] by Richard Riehle - Ada

This book is aimed at experienced programmers who want to learn Ada at the corresponding section of the Ada Language Reference Manual (ALRM) in the 



[PDF] Reference Manual for the Ada Programming Language - DTIC

Ada Tokyo Study Group These reviews and The standard definition of the Ada programming language consists of the fourteen chapters and the three The description of the language definition In thit standard reference manual uses the  



[PDF] ADA - Caribbean Environment Programme - UNEP

presentation of modern programming concepts and Ada 95 language capabilities learn programming fundamentals and the C programming language This Reference Manual in printable and hypertext formats * Graphical syntax charts "I  



[PDF] Ada A quick crash course

8 sept 2014 · gramming experience and uses Ada as the programming language It's assumed that a vast majority of the ones reading this tutorial already



[PDF] Thoughts on Ada and Language Technology in Our Times - Ada 2012

A programming language cannot be successful in all of these domains Ada is designed for tutorials depending on the concepts each tutorial wants to convey

[PDF] learn c pdf download

[PDF] learn c pdf download free

[PDF] learn cbse ncert solutions class 9th hindi sparsh

[PDF] learn chinese cantonese language

[PDF] learn cw morse code

[PDF] learn dart for flutter

[PDF] learn english in 30 days pdf

[PDF] learn enough rails

[PDF] learn flutter basics

[PDF] learn kinyarwanda app

[PDF] learn korean pdf with audio

[PDF] learn lua language pdf

[PDF] learn machine language pdf

[PDF] learn qbasic programming language pdf

[PDF] learn robotics programming pdf

Ada-95: A guide for C and C++ programmers

by Simon Johnston 1995

Welcome

... to the Ada guide especially written for C and C++ programmers.

Summary

I have endeavered to present below a tutorial for C and C++ programmers to show them what Ada can provide and how to set about turning the knowledge and experience they have gained in C/C++ into good Ada programming. This really does expect the reader to be familiar with C/C++, although C only programmers should be able to read it OK if they skip section 3. My thanks to S. Tucker Taft for the mail that started me on this. 1

Contents

1 Ada Basics.7

1.1 C/C++ types to Ada types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

1.1.1 Declaring new types and subtypes. . . . . . . . . . . . . . . . . . . . . . . . 8

1.1.2 Simple types, Integers and Characters. . . . . . . . . . . . . . . . . . . . . . 9

1.1.3 Strings.{3.6.3}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.1.4 Floating{3.5.7}and Fixed{3.5.9}point. . . . . . . . . . . . . . . . . . . . . 10

1.1.5 Enumerations{3.5.1}and Ranges. . . . . . . . . . . . . . . . . . . . . . . . 11

1.1.6 Arrays{3.6}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

1.1.7 Records{3.8}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

1.1.8 Access types (pointers){3.10}. . . . . . . . . . . . . . . . . . . . . . . . . . 16

1.1.9 Ada advanced types and tricks. . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.1.10 C Unions in Ada, (food for thought). . . . . . . . . . . . . . . . . . . . . . . 22

1.2 C/C++ statements to Ada. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

1.2.1 Compound Statement{5.6}. . . . . . . . . . . . . . . . . . . . . . . . . . . 24

1.2.2 if Statement{5.3}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

1.2.3 switch Statement{5.4}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

1.2.4 Ada loops{5.5}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

1.2.4.1 while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

1.2.4.2 do Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

1.2.4.3 for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

1.2.4.4 break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . 27

1.2.5 return{6.5}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

1.2.6 labels and goto{5.8}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

1.2.7 exception handling{11.2}. . . . . . . . . . . . . . . . . . . . . . . . . . . 29

1.2.8 sub-programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

1.3 Ada Safety. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

2

CONTENTS CONTENTS

1.3.1 Static provability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

1.3.2 Predefined exceptions and pragmas. . . . . . . . . . . . . . . . . . . . . . . 34

1.3.3 Unchecked programming. . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

2 Ada Packages.{7}37

2.1 What a package looks like . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

2.2 Include a package in another . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

2.3 Package data hiding{7.3}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

2.4 Hierarchical packages. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

2.5 Renaming identifiers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

3 Ada-95 Object Oriented Programming. 43

3.1 The tagged type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

3.2 Class member attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

3.3 Class member functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

3.4 Virtual member functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

3.5 Static members. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

3.6 Constructors/Destructors for Ada. . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

3.7 Inheritance, single and multiple. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

3.8 public/protected/private. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

3.9 A more complete example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

4 Generics49

4.1 A generic procedure{12.6}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

4.2 Generic packages{12.7}. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

4.3 Generic types and other parameters{12.4}. . . . . . . . . . . . . . . . . . . . . . . 51

5 IO52

5.1 Ada.Text_IO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

5.2 Ada.Sequential_IO and Ada.Direct_IO . . . . . . . . . . . . . . . . . . . . . . . . . 53

5.3 Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

6 Interfacing to other languages 56

3

CONTENTS CONTENTS

7 Concurrency 57

7.1 Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

7.1.1 Tasks as threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

7.1.2 A Simple task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

7.1.3 Task as types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

7.2 Task synchronization (Rendezvouz) . . . . . . . . . . . . . . . . . . . . . . . . . . 58

7.2.1 entry/accept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

7.2.2 select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

7.2.3 guarded entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

7.2.4 delays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

7.2.5 select else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

7.2.6 termination . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

7.2.7 conditional entry calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

7.3 Protected types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

4

Introduction.

This document is written primarily for C and C++ programmers and is set out to describe the Ada programminglanguageinawaymoreaccessibletothem. IhaveusedthestandardAdadocumentation conventions, code will look likethisand keywords will look likethis. I will include references to the Ada Reference Manual in braces and in italics,{1.1}, which denotes section 1.1. The ARM

is reference 1 at the end of this document. Another useful reference is the Lovelace on-line tutorial

which is a great way to pick up Ada basics. I will start out by describing the Ada predefined types, and the complex types, and move onto the simple language constructs. Section 2 will start to introduce some very Ada specific topics and section 3 describes the new Ada-95 Object Oriented programming constructs. Section 5 describes the Ada tools for managing concurrency, the task and protected types, these are worth investing some

time getting to grips with. Section 6 is a tour of the Ada IO library and covers some of the differences

in concept and implementation between it and C stdio.

Please feel free to comment on errors, things you don"t like and things you would like to see. If I don"t

get the comments then I can"t take it forward, and the question you would like answered is almost certainly causing other people problems too. If you are new to Ada and do not have an Ada compiler handy then why not try the GNAT Ada compiler. This compiler is based on the well known GCC C/C++ and Objective-C compiler and provides a high quality Ada-83 and Ada-95 compiler for many platforms. Here is the FTP site (ftp: //cs.nyu.edu/pub/gnat) see if there is one for you. 5

Document Status.

This document is still under revision and I receive a number of mails asking for improvements and

fixing bugs and spelling mistakes I have introduced. I will try and keep this section up to date on what

needs to be done and what I would like to do.

Current Status

Section 2More on 2.3 (data hiding) and 2.4 (Hierarchical packages) Section 3First issue of this section, 3.6, 3.7, 3.8 and 3.9 have additional work planned. They may also require re-work pending comments.

Section 5Section 5.3 (streams) not yet done.

Section 6New sections to be added for each language. Section 7Major re-work following comments from Bill Wagner, 7.2.7 added, requires some more words, and section 7.3 requires more justification etc.

Wish List

I would like to use a consistant example throughout, building it up as we go along. The trouble is I don"t think I have space in an HTML page to do this. 6 1

Ada Basics.

This section hopes to give you a brief introduction to Ada basics, such as types, statements and

packages. Once you have these you should be able to read quite a lot of Ada source without difficulty.

You are expected to know these things as we move on so it is worth reading. One thing before we continue, most of the operators are similar, but you should notice these differ- ences:OperatorC/C++Ada

Assignment=:=

Equality===

NonEquality!=/=

PlusEquals+=

SubtractEquals-=

MultiplyEquals*=

DivisionEquals/=

OrEquals|=

AndEquals&=

Modulus%mod

Remainderrem

AbsoluteValueabs

Exponentiation**

Range. .

One of the biggest things to stop C/C++ programmers in their tracks is that Ada is case insensitive, sobegin BEGIN Beginare all the same. This can be a problem when porting case sensitive C code into Ada.

Another thing to watch for in Ada source is the use of " the tick. The tick is used to access attributes

for an object, for instance the following code is used to assign to value a the size in bits of an integer.

int a = sizeof(int) * 8; a : Integer := Integer"Size; Another use for it is to access the attributesFirstandLast, so for an integer the range of possible values isInteger"FirsttoInteger"Last. This can also be applied to arrays so if you are 7

1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS.

passed an array and don"t know the size of it you can use these attribute values to range over it in a

loop (see section 1.1.5 on page 11). The tick is also used for other Ada constructs as well as attributes,

for example character literals, code statements and qualified expressions ( 1.1.8 on page 16).

1.1 C/C++ types to Ada types.

This section attempts to outline how to move C/C++ type declarations into an Ada program and help you understand Ada code. Section 1.1.8 introduces some Ada specific advanced topics and tricks you can use in such areas as bit fields, type representation and type size.

Note that "objects" are defined in reverse order to C/C++, the object name is first, then the object type,

as in C/C++ you can declare lists of objects by seperating them with commas. int i; int a, b, c; int j = 0; int k, l = 1; i : Integer; a, b, c : Integer; j : Integer := 0; k, l : Integer := 1;

The first three declarations are the same, they create the same objects, and the third one assigns j the

value 0 in both cases. However the fourth example in C leaves k undefined and creates l with the value 1. In the Ada example it should be clear that both k and l are assigned the value 1.

Another difference is in defining constants.

const int days_per_week = 7; days_per_week :constantInteger := 7; days_per_week :constant:= 7; In the Ada example it is possible to define a constant without type, the compiler then chooses the most appropriate type to represent it.

1.1.1 Declaring new types and subtypes.

Before we delve into descriptions of the predefined Ada types it is important to show you how Ada defines a type. Ada is a strongly typed language, in fact possibly the strongest. This means that its type model is strict and absolutely stated. In C the use of typedef introduces a new name which can be used as a new type, though the weak typing of C and even C++ (in comparison) means that we have only really introduced a very poor synonym. Consider: typedef int INT;

INT a;

int b; a = b; // works, no problem 8

1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS.

The compiler knows that they are both ints. Now consider: typeINTis newInteger; a : INT; b : Integer; a := b; -- fails. The important keyword isnew, which really sums up the way Ada is treating that line, it can be read as "a new typeINThas been created from the typeInteger", whereas the C line may be interpreted as "a new nameINThas been introduced as a synonym forint". This strong typing can be a problem, and so Ada also provides you with a feature for reducing the distance between the new type and its parent, consider: subtypeINTisInteger; a : INT; b : Integer; a := b; -- works.

The most important feature of the subtype is to constrain the parent type in some way, for example to

place an upper or lower boundary for an integer value (see section below on ranges).

1.1.2 Simple types, Integers and Characters.

We have seen above the Integer type, there are a few more with Ada, these are listed below. Integer, Long_Integer etc.Any Ada compiler must provide the Integer type, this is a signed integer, andofimplementationdefinedsize. ThecompilerisalsoatlibertytoprovideLong_Integer,

Short_Integer, Long_Long_Integeretc as needed.

Unsigned IntegersAda does not have a defined unsigned integer, so this can be synthesised by a rangetype(seesection1.1.5), andAda-95hasadefinedpackage,System.Unsigned_Types which provide such a set of types. Ada-95 has added amodulartype which specifies the maximum value, and also the feature that arithmatic is cyclic, underflow/overflow cannot occur. This means that if you have a modular type capable of holding values from 0 to 255, and its current value is 255, then incrementing it wraps it around to zero. Contrast this with range types (previously used to define unsigned integer types) in section 1.1.5 below. Such a type is defined in the form: typeBYTEis mod256; typeBYTEis mod2**8; The first simply specifies the maximum value, the second specifies it in a more "precise" way, and the 2**x form is often used in system programming to specify bit mask types. Note: it is not required to use 2**x, you can use any value, so 10**10 is legal also. Character{3.5.2}ThisisverysimilartotheCchartype, andholdstheASCIIcharacterset. However it is actually defined in the packageStandard{A.1} as an enumerated type (see section

1.1.5). There is an Ada equivalent of the C set of functions inctype.hwhich is the package

Ada.Characters.Handling.

Ada Also defines aWide_Charactertype for handling non ASCII character sets. 9

1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS.

Boolean{3.5.3}This is also defined in the packageStandardas an enumerated type (see below) as (FALSE, TRUE).

1.1.3 Strings.{3.6.3}

HeresagodsendtoC/C++programmers, AdahasapredefinedStringtype(definedagaininStandard).

There is a good set of Ada packages for string handling, much better defined than the set provided by

C, and Ada has a & operator for string concatenation.

As in C the basis for the string is an array of characters, so you can use array slicing (see below) to

extract substrings, and define strings of set length. What, unfortunatly, you cannot do is use strings as

unbounded objects, hence the following. typeA_Recordis record illegal : String; legal : String(1 .. 20); end record; procedurecheck(legal :inString);

The illegal structure element is because Ada cannot use "unconstrained" types in static declarations,

so the string must be constrained by a size. Also note that the lower bound of the size must be greater

than or equal to 1, the C/C++array[4]which defines a range0..3cannot be used in Ada,1..4 must be used. One way to specify the size is by initialisation, for example:

Name : String := "Simon";

is the same as definingNameas aString(1..5)and assigning it the value"Simon"seperatly.. For parameter types unconstrained types are allowed, similar to passingint array[]in C. which implements a variable length string type.

1.1.4 Floating{3.5.7}and Fixed{3.5.9}point.

Ada has two non-integer numeric types, the floating point and fixed point types. The predefined floating point type isFloatand compilers may addLong_Float, etc. A new Float type may be defined in one of two ways: typeFloatingPoint1is newFloat; typeFloatingPoint2isdigits 5; The first simply makes a new floating point type, from the standardFloat, with the precision and size of that type, regardless of what it is.

The second line asks the compiler to create a new type, which is a floating point type "of some kind"

with a minimum of 5 digits of precision. This is invaluable when doing numeric intensive operations 10

1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS.

and intend to port the program, you define exactly the type you need, not what you think might do today.

If we go back to the subject of the tick, you can get the number of digits which are actually used by

the type by the attribute"Digits. So having said we want a type with minimum of 5 digits we can verify this: number_of_digits : Integer := FloatingPoint2"Digits; Fixed point types are unusual, there is no predefined type "Fixed" and such type must be declared in the long form: typeFixedis delta0.1range-1.0..1.0; This defines a type which ranges from -1.0 to 1.0 with an accuracy of 0.1. Each element, accuracy, low-bound and high-bound must be defined as a real number. There is a specific form of fixed point types (added by Ada-95) called decimal types. These add a clause digits, and the range clause becomes optional. typeDecimalis delta0.01digits10;

This specifies a fixed point type of 10 digits with two decimal places. The number of digits includes

the decimal part and so the maximum range of values becomes -99,999,999.99...+99,999,999.99

1.1.5 Enumerations{3.5.1}and Ranges.

Firstly enumerations. These are not at all like C/C++s enums, they are true sets and the fact that the

Boolean type is in fact:

typeBooleanis(FALSE, TRUE); should give you a feeling for the power of the type. You have already seen a range in use (for strings), it is expressed aslow .. highand can be one of the most useful ways of expressing interfaces and parameter values, for example: typeHoursis newIntegerrange1 .. 12; typeHours24is range0 .. 23; typeMinutesis range1 .. 60; There is now no way that a user can pass us an hour outside the range we have specified, even to the extent that if we define a parameter of typeHours24we cannot assign a value ofHourseven though it can only be in the range. Another feature is demonstrated, forHourswe have said we want to restrict anIntegertype to the given range, for the next two we have asked the compiler

to choose a type it feels appropriate to hold the given range, this is a nice way to save a little finger

tapping, but should be avoided Ada provides you a perfect environment to specify precisely what you want, use it the first definition leavesnothingto the imagination. Now we come to the rules on subtypes for ranges, and we will define the twoHoursagain as follows: 11

1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS.

typeHours24is new range0..23; subtypeHoursisHours24range1..12; This limits the range even further, and as you might expect a subtype cannot extend the range beyond its parent, sorange 0 .. 25would have been illegal. Now we come to the combining of enumerations and ranges, so that we might have: typeAll_Daysis(Monday, Tuesday, Wednesday, Thursday,

Friday, Saturday, Sunday);

subtypeWeek_DaysisAll_DaysrangeMonday .. Friday; subtypeWeekendisAll_DaysrangeSaturday .. Sunday; We can now take aDay, and see if we want to go to work:

Day : All_Days := Today;

ifDay in Week_Daysthen go_to_work; end if; Or you could use the formifDayin rangeMonday .. Fridayand we would not need the extra types.

Ada provides four useful attributes for enumeration type handling, note these are used slightly differ-

ently than many other attributes as they are applied to the type, not the object. SuccThis attribute supplies the "successor" to the current value, so the "Succvalue of an object containingMondayisTuesday. Note: If the value of the object isSundaythen an exception is raised, you cannotSuccpast the end of the enumeration. PredThis attribute provides the "predecessor" of a given value, so the"Predvalue of an object containingTuesdayisMonday. Note: the rule above still applies"PredofMondayis an error. ValThis gives you the value (as a member of the enumeration) of element n in the enumeration. Thus

Val(2)isWednesday.

Note: the rule above still applies, and note also that "Val(0)is the same as "First. PosThisgivesyouthepositionintheenumerationofthegivenelementname. Thus"Pos(Wednesday) is2. Note: the range rules still apply, also that"Lastwill work, and returnSunday.

All_Days"Succ(Monday) = Tuesday

All_Days"Pred(Tuesday) = Monday

All_Days"Val(0) = Monday

All_Days"First = Monday

All_Days"Val(2) = Wednesday

All_Days"Last = Sunday

All_Days"Succ(All_Days"Pred(Tuesday)) = Tuesday

12

1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS.

Ada also provides a set of 4 attributes for range types, these are intimatly associated with those above

and are: FirstThis provides the value of the first item in a range. Considering the range0 .. 100then "Firstis0. LastThis provides the value of the last item in a range, and so considering above,"Lastis100. LengthThis provides the number of items in a range, so"Lengthis actually101. RangeThis funnily enough returns in this case the value we gave it, but you will see when we come onto arrays how useful this feature is. As you can see these have no direct C/C++ equivalent and are part of the reason for Ada"s reputation

for safety, you can define for a parameter exactly the range of values it might take, it all amounts to

better practice for large developments where your interface is read by many people who may not be able to tell that the integer parameter day starts at 0, which indicates Wednesday etc.

1.1.6 Arrays{3.6}.

Arrays in Ada make use of the range syntax to define their bounds and can be arrays of any type, and can even be declared as unknown size.

Some example:

char name[31]; int track[3]; int dbla[3][10]; int init[3] = { 0, 1, 2 }; typedef char[31] name_type; track[2] = 1; dbla[0][3] = 2;

Name :array(0 .. 30)ofCharacter; -- OR

Name : String (1 .. 30);

Track :array(0 .. 2)ofInteger;

DblA :array(0 .. 2)of array(0 .. 9)ofInteger; -- OR

DblA :array(0 .. 2,0 .. 9)ofInteger;

Init :array(0 .. 2)ofInteger := (0, 1, 2);

typeName_Typeis array(0 .. 30)ofCharacter; track(2) := 1; dbla(0,3) := 2; -- Note try this in C. a, b : Name_Type; a := b; -- will copy all elements of b into a. Simple isn"t it, you can convert C arrays into Ada arrays very easily. What you don"t get is all the things you can do with Ada arrays that you can"t do in C/C++. 13

1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS.

non-zero based ranges.Because Ada uses ranges to specify the bounds of an array then you can easily set the lower bound to anything you want, for example:

Example :array(-10 .. 10)ofInteger;

non-integer ranges.In the examples above we have used the common abbreviation for range spec-

ifiers. The ranges above are all integer ranges, and so we did not need to use the correct form which

is: array(typerangelow .. high) which would make Example abovearray(Integerrange-10 .. 10). Now you can see where we"re going, take an enumerated type,All_Daysand you can define an array: Hours_Worked :array(All_DaysrangeMonday .. Friday); unbounded array types.The examples above did demonstrate how to declare an array type. One

of Ada"s goals is reuse, and to have to define a function to deal with a 1..10 array, and another for a

0..1000 array is silly. Therefore Ada allows you to define unbounded array types. An unbounded type

can be used as a parameter type, but you cannot simply define a variable of such a type. Consider: typeVectoris array(Integerrange<>)ofFloat; proceduresort_vector(sort_this :in outVector);

Illegal_Variable : Vector;

Legal_Variable : Vector(1..5);

subtype SmallVectorisVector(0..1);

Another_Legal : SmallVector;

This does allow us great flexibility to define functions and procedures to work on arrays regardless of their size, so a call tosort_vectorcould take theLegal_Variableobject or an object of typeSmallVector, etc.Notethat a variable of typeSmallvectoris constrained and so can be legally created. array range attributes.If you are passed a type which is an unbounded array then if you want to loop through it then you need to know where it starts. So we can use the range attributes introduced in 1.1.5 to iterate over a given array thus: attributes for array types. Consider:

Example :array(1 .. 10)ofInteger;

foriinExample"First .. Example"Lastloop foriinExample"Rangeloop Note that if you have a multiple dimension array then the above notation implies that the returned values are for the first dimension, use the notationArray_Name"attribute(dimension)for multi-dimensional arrays. 14

1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS.

Initialisation by range (Aggregates{}???)When initialising an array one can initialise a range of elements in one go:

Init :array(0 .. 3)ofInteger := (0 .. 3 => 1);

Init :array(0 .. 3)ofInteger := (0 => 1,others=> 0); The keywordotherssets any elements not explicitly handled. SlicingArray slicing is something usually done with memcpy in C/C++. Take a section out of one array and assign it into another.

Large :array(0 .. 100)ofInteger;

Small :array(0 .. 3)ofInteger;

-- extract section from one array into another.

Small(0 .. 3) := Large(10 .. 13);

-- swap top and bottom halfs of an array.

Large := Large(51 .. 100) & Large(1..50);

Note: Both sides of the assignment must be of the same type, that is the same dimensions with each element the same. The following is illegal. -- extract section from one array into another.

Small(0 .. 3) := Large(10 .. 33);

-- ^^^^^^^^ range too big.

1.1.7 Records{3.8}.

You shouldn"t have too much problem here, you can see an almost direct mapping from C/C++ to Ada for simple structures. Note the example below does not try to convert type to type, thus the C char*, to hold a string is converted to the Ada String type. struct _device { int major_number; int minor_number; char name[20]; typedef struct _device Device; typestruct_deviceis record major_number : Integer; minor_number : Integer; name : String(1 .. 19); end record; typeDeviceis newstruct_device; 15

1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS.

As you can see, the main difference is that the name we declare for the initial record is a type, and

can be used from that point on. In C all we have declared is a structure name, we then require the additional step of typedef-ing to add a new type name. Ada uses the same element reference syntax as C, so to access the minor_number element of an object lp1 of type Device we writelp1.minor_number. Ada does allow, like C, the initialisation of record members at declaration. In the code below we introduce a feature of Ada, the ability to

name the elements we are going to initialise. This is useful for clarity of code, but more importantly

it allows us to only initialise the bits we want.

Device lp1 = {1, 2, "lp1"};

lp1 : Device := (1, 2, "lp1"); lp2 : Device := (major_number => 1, minor_number => 3, name => "lp2"); tmp : Device := (major_number => 255, name => "tmp"); When initialising a record we use an aggregate, a construct which groups together the members. This facility (unlike aggregates in C) can also be used to assign members at other times as well. tmp : Device; -- some processingquotesdbs_dbs20.pdfusesText_26