Topic#12 - CS162 Topic#12 • Lecture – Arrays with...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS162 - Topic #12 • Lecture: – Arrays with Structured Elements • defining and using arrays of arrays • remember pointer arithmetic • Programming Project – Any questions? CS162 - Subscript Operator • The subscript operator provides access to individual elements of an array. • The subscript operator is a binary operator. • The first operand is an expression designating the address of the first element of an array; this can be the array identifier, or as we will see later, this can also be a pointer expression. • The second operand is an integer expression contained within the brackets designating the element of the array to be accessed. • The first element of an array always begins at index zero and the last element of an array ends at the index that is one less than the size of the array; thus, legal indices fall within the range 0 through size-1. CS162 - Subscript Operator //for some statcially allocated array: cout <<"address of array is " <<array << endl; for(int i=0; i<size; ++i) //loop for i=0...6 cout <<"array[" <<i <<"] equals " <<array[i] << endl; • Each of the elements of the array sequentially follow one another in memory. • We can think of each element of an array as an unnamed variable that we identify by using an index. • The actual address of each element is computed by the subscript operator and takes into the account the size of the elements in the array. • The index is independent of the actual address of each element. CS162 - sizeof Operator • The only other operator that can be directly applied to arrays is the sizeof operator. • The sizeof operator returns the number of characters (bytes) that an array occupies. • The number of elements in an array can be determined by dividing the size of the array by the size of an element in the array. cout <<"size of int array = " <<sizeof(array) <<endl; cout <<"size of int = " <<sizeof(int) <<endl; cout <<"number of elements in the array = " <<sizeof(array)/sizeof(array[0]) << endl; CS162 - Pointers and Arrays • We can also use pointers to point to data that is stored sequentially in memory. • We can treat a pointer to data stored sequentially in memory as an array. • All operations on arrays have an equivalent pointer representation. • We can take advantage of this to improve our programs' performance when operating on arrays. CS162 - Pointers and Arrays • It is possible to define the behavior of the subscript operator entirely in terms of operations on a pointer. • The first thing we need to know is that the identifier of an array is a constant pointer to the first element of that array....
View Full Document

This note was uploaded on 03/17/2010 for the course CS 162 taught by Professor Fant during the Spring '08 term at Portland State.

Page1 / 24

Topic#12 - CS162 Topic#12 • Lecture – Arrays with...

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

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