06+-+Arrays+and+Pointers

06+-+Arrays+and+Pointers - On to Arrays Data Types weve...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
9/27/2009 1 Arrays and Pointers EECS 280 Programming and Introductory Data Structures On to Arrays Data Types we’ve seen so far… c “atoms”: types with singleton values. c For example, something of type “int” holds one “integer”. c Something of type “char” holds one character. c “aggregate” types: types that hold “sets” of values where each value is of some type. c When all values have the same type, we call it a “homogeneous” aggregate, otherwise it is “heterogeneous”. c These are sometimes also called “container” types, because they contain other types. c The two we've seen so far, in the context of project 2, are lists and trees: each holds a collection of integers. On to Arrays Revisiting Lists c Lists have an important restriction (i.e. you cannot talk about an arbitrary element of a list). You can only talk about “the first element” and “the rest of the list”. c We say such a structure is “linked”; you have to move from one link to the next. c For example, suppose we have the following list: ( 1 2 3 4 ) c To talk about element “4”, you have to call list_rest three times (to skip past 1, 2, and 3), and then call list_first. c Lists have an important advantage too: c Because they are defined recursively, there is no "maximum- sized" list. c A list can grow as large as it needs to be (assuming the resources of the computer are infinite). On to Arrays Revisiting Lists Arrays The C++ array structure c In contrast to a list, C++ has a structure called an array . c An array is a fixed-sized, indexed, homogeneous aggregate type (a collection of items, all of the same type.) c To declare and define an array of four integers (the same size as our list above), we would say the following: int array[4]; Arrays The C++ array structure c For comparison purposes, let's also declare and define an integer, foo: int foo; int array[4]; c The environment that we get when we do this is: foo: array:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
9/27/2009 2 Arrays Question: The C++ array structure int foo; int array[4]; c What are the contents of “array” after this declaration? foo: array: Arrays Initializing an array c You can also initialize the contents of an array in one line – just like with an int. However, we need some sort of notation to specify a set of numbers: int foo = 7; int array[4] = { 1, 2, 3, 4 }; c The corresponding environment would look like this: foo: array: 7 4 3 2 1 Arrays Initializing an array c You can also initialize the contents of an array in one line – just like with an int. However, we need some sort of notation to specify a set of numbers: int foo = 7; int array[4] = { 1, 2, 3, 4 }; c The corresponding environment would look like this: foo: array: 7 4 3 2 1 This is called a “static initializer”. Arrays
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

06+-+Arrays+and+Pointers - On to Arrays Data Types weve...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online