CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?

Slides:



Advertisements
Similar presentations
Pointers and Arrays Chapter 12
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Programming and Data Structure
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
Pointers Applications
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
Spring 2005, Gülcihan Özdemir Dağ Lecture 7, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 7 Outline 7. 1.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
POINTERS.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointers It provides a way of accessing a variable without referring to its name. The mechanism used for this is the address of the variable.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Pointers and Pointer-Based Strings
Pointers and References
Lecture 18 Arrays and Pointer Arithmetic
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
Pointers Lecture 2 Tue, Jan 24, 2006.
Pointers Lecture 1 Thu, Jan 15, 2004.
Overloading functions
Pointers and Pointer-Based Strings
C++ Programming Lecture 18 Pointers – Part II
Pointers and References
Pointers and pointer applications
Presentation transcript:

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. It is a pointer to the same type as the elements of the array. This means that we can initialize or assign an array name to a pointer, where the pointer points to data of the same type as the elements of our array.

CS162 - Pointers and Arrays int ai[7]; //ai is of type pointer to int int* pi; //pi is a pointer to int pi = ai; //pi now points to the array ai We now have two ways to access elements of an array, one using the name of the array (ai) and the other using a pointer (pi). In this example, the name of the array (ai) is a constant pointer to an int. The pointer (pi) is a variable pointer to an int that has been assigned the same address as ai.

CS162 - Pointers and Arrays Since the value of ai has been assigned to pi, the residual value of using either ai or pi in an expression is the same in either case: –it is the address of the first element of the array. When ai is used in an expression, that expression uses the value that the constant ai represents. When pi is used in an expression, that expression uses the value currently assigned to variable pi. We can apply the subscript operator to this residual value (an expression of type pointer to an int) in order to access the elements of the array.

CS162 - Pointers and Arrays int *pi; //pi is a pointer to an int pi = ai; //same as pi = &a[0] for(int i=0; i<7; i++) if(ai[i] != pi[i]) cout <<"Oops - big trouble in River City" <<endl; The relationship between pointers and arrays is defined by the following identity, where E1 is a pointer (either an array name or a pointer expression) and E2 is an integer expression. E1[E2] == *((E1)+(E2))

CS162 - Pointers and Arrays ai[3] = 42; //this stores 42 w/array subscripting *(ai+3)=42; //same thing using pointer operations *(3+ai)=42; //addition is communitive 3[ai] = 42; //this works! This identity means that the subscript operation is equivalent to adding the index to the pointer expression and then dereferencing the result. Understanding this identity allows us to decompose array subscripting operations into pointer operations. Array and pointer operations can be the same, even though the declarations for arrays and pointers are different.

CS162 - Pointers and Arrays This identity means that the subscript operation is equivalent to adding the index to the pointer expression and then dereferencing the result. Understanding this identity allows us to decompose array subscripting operations into pointer operations. Array and pointer operations can be the same, even though the declarations for arrays and pointers are different. ai[3] = 42; //this stores 42 w/array subscripting *(ai+3)=42; //same thing using pointer operations *(3+ai)=42; //addition is communitive 3[ai] = 42; //this works! All of the above works as well if pi were used instead!

CS162 - Pointers and Arrays We have seen that the name of an array can be replaced with a pointer to the first element of the array. The only difference is that the name of an array is a constant and cannot be modified, whereas a pointer can be defined as a variable and therefore can be modified. The process of modifying a pointer variable is called pointer arithmetic.

CS162 - Pointer Arithmetic Walk through the following in class: int a[10]; int* p=a; //initialize p to &a[0] int* q=&a[2]; //initialize q to &a[2] p = q; //assign q to p p = &a[5]; //p points to the 6th element &a[5] p+=3; //p now points to the 9th element &a[8] p-=8; //p now points to the 1st element &a[0] p = &a[5]; //p points to the 6th element &a[5] ++p; //p now points to the 7th element &a[6] p++; //p now points to the 8th element &a[7] p = p + 2; //p now points to the 10th element &a[9] --p; //p now points to the 9th element &a[8] p--; //p now points to the 8th element &a[7] p = p - 2; //p now points to the 6th element &a[5]

CS162 - Pointer Arithmetic There are two key points in understanding pointer arithmetic. The first is that pointer variables can be modified whereas array names are constants and cannot be modified. The second is that pointer operations automatically take into account the size of the data pointed to, just like array subscripts do. This means that operations such as addition and subtraction are independent of the size of the data. When we add one to a pointer of some type, we point to the next element of that type.

CS162 - Pointer Arithmetic Walk through the following in class: int a[10]; int* p=a; //define and initialize p to &a[0] p = p + 1; //add 1 to p; p==&a[1] *p = *p + 1; //(*p)=(*p)+1; add 1 to a[1] *p = *(p + 1); //copy a[2] to a[1] p+=1; //add 1 to p; p==&a[2] *p+=1; //(*p)+=1; add 1 to a[2] *(p+=1); //add 1 to p; p==&a[3] ++p; //add 1 to p; p==&a[4] ++*p; //derefer p; add 1 to a[4] *++p; //add 1 to p; p==&a[5] p++; //add 1 to p; p==&a[6] *p++; //*(p++); rvalue==6; add 1 to p; p==&a[7] (*p)++; //dereference p; add 1 to a[7]

CS162 - Pointer Arithmetic

CS162 - Arrays of Arrays Arrays can be formed from any type of data, even other arrays! When each element is an array, we define an array of arrays. With an array of arrays, each element is an array of some type. Arrays of arrays are sometimes called multidimensional arrays in C++. This is not strictly correct because each dimension represents a different type, rather than each dimension representing the same type.

CS162 - Arrays of Arrays An array of arrays is defined just like an array of a fundamental type, except that the identifier is immediately followed by an additional pair of brackets ([]). The size of each element's array, called a subarray, is supplied within the second set of brackets as a literal, constant, or constant expression. int array[3][2];

CS162 - Arrays of Arrays To access elements of an array of arrays, we can use the subscript operator. To access the appropriate subarray, we follow the name of the array by an index in brackets. For example, array[0] accesses the first subarray. The value of this element is the first subarray of two integers. Its type is an array of integers (a pointer to an int). To access elements within a subarray, we follow the name of the array by the index of the subarray in brackets and then follow that by the index of the element within the subarray that we wish to access in brackets. For example, array[0][0] accesses the first integer in the first subarray.

CS162 - Arrays of Arrays The name of an array of arrays also represents a pointer expression. By itself, it has a value equal to the address of the first element of the array. The type is a pointer to the first element of the array. For example, the type of array is int (*)[2] (a pointer to an array of two integers). int array[3][2]; int (*p1)[2]; //define pointer of same type as array p1 = array; //assign pointer to point to array

CS162 - Array of Arrays Walk through the following in class: int array[6][2]; int (*p1)[2]; //define pointer of same type as array p1 = array; //assign pointer to point to array int *p; //define ptr of same type as subarray p = *p1; //assign ptr to point to 1st subarray p = array[0]; //this also points to 1st subarray and p = *(array+0); //so does this because of our identity p = *array; //and so does this We can define and initialize a pointer to a subarray. The type of a subarray is a pointer to an element of the subarray. By defining a pointer of that type, we can use pointer arithmetic to access the subarray.

CS162 - Array of Arrays

CS162 - Arrays of Arrays If we were to print the value of the pointers p1 and p, their values would be the same even though they are different types. This is because the address of the first element of array is at the same address as the first element in the first subarray. However, when we add to or subtract from these two pointers, the results are significantly different. By adding one to pointer p1, we point to the next subarray of 2 integers. By adding one to pointer p, we point to the next int within the first subarray.

CS162 - For Next Time Review the study guide Come prepared with questions –regarding the last stage of the assignment –regarding the final exam