06+-+Arrays+and+Pointers

06+-+Arrays+and+Pointers - EECS 280 Programming and...

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

View Full Document Right Arrow Icon
Arrays and Pointers EECS 280 Programming and Introductory Data Structures
Background image of page 1

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

View Full DocumentRight Arrow Icon
On to Arrays Data Types we’ve seen so far… “atoms”: types with singleton values. For example, something of type “int” holds one “integer”. Something of type “char” holds one character. “aggregate” types: types that hold “sets” of values where each value is of some type. When all values have the same type, we call it a “homogeneous” aggregate, otherwise it is “heterogeneous”.
Background image of page 2
On to Arrays Revisiting Lists 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”. We say such a structure is “linked”; you have to move from one link to the next. For example, suppose we have the following list: ( 1 2 3 4 )
Background image of page 3

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

View Full DocumentRight Arrow Icon
Lists have an important advantage too: Because they are defined recursively, there is no "maximum-sized" list. A list can grow as large as it needs to be (assuming the resources of the computer are infinite). On to Arrays Revisiting Lists
Background image of page 4
Arrays The C++ array structure In contrast to a list, C++ has a structure called an array . An array is a fixed-sized, indexed, homogeneous aggregate type (a collection of items, all of the same type.) To declare and define an array of four integers (the same size as our list above), we would say the following:
Background image of page 5

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

View Full DocumentRight Arrow Icon
Arrays The C++ array structure For comparison purposes, let's also declare and define an integer, foo: int foo; int array[4]; The environment that we get when we do this is: foo: array:
Background image of page 6
Arrays Question: The C++ array structure int foo; int array[4]; What are the contents of “array” after this declaration? foo: array:
Background image of page 7

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

View Full DocumentRight Arrow Icon
Arrays Initializing an array 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 }; The corresponding environment would look like this: foo: array: 7 4 3 2 1
Background image of page 8
Arrays Initializing an array 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 }; The corresponding environment would look like this: foo: array: 7 4 3 2 1 This is called a “static initializer”.
Background image of page 9

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

View Full DocumentRight Arrow Icon
Arrays Accessing elements in an array You can access the contents of an array using an “index”. The index of the first array element is zero, the next is one, and so on. So, we can name the individual elements of array, like so: foo: array: 7 4 3 2 1 array[0] array[1] array[2] array[3]
Background image of page 10
Arrays Accessing elements in an array Each individual element is used just like a regular int, so all of the following are legal: foo: array: 7 4 3 2 1 array[0] array[1] array[2] array[3]
Background image of page 11

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

View Full DocumentRight Arrow Icon
Arrays Accessing elements in an array
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/19/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 39

06+-+Arrays+and+Pointers - EECS 280 Programming and...

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

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