CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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 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.
Chapter 10.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
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.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
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.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
CS 31 Discussion, Week 10 Faisal Alquaddoomi, Office Hours: BH 2432, F 12:30-1:30pm.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
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.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers *, &, array similarities, functions, sizeof.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
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.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 5: Pointer Outline Chapter 5 Pointer continue Call by reference Pointer arithmatic Debugging.
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.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Arrays Hold Multiple Values 7.1.
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.
Java Programming Language Lecture27- An Introduction.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Student Book An Introduction
Lecture 18 Arrays and Pointer Arithmetic
Java Programming Language
Pointers and dynamic objects
CS31 Discussion 1H Fall18: week 7
Presentation transcript:

CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm

C-Strings Review C-Strings are just char arrays, with some convenient functions for manipulating them – End in a ‘nul-terminator’ that signifies the end of the string C-Strings have special manipulation functions in the library

2D Arrays Review 2D arrays are like 1D arrays, except they have an extra dimension – by convention, the first dimension is the row, the second is the column – declare: int myArray[5][10]; // 5 rows, 10 columns – access/change: myArray[2][3] = 888; // ^ item at row 2, col 3 is now 888 2D arrays can be statically allocated, but the second dimension (columns) must be specified Similarly, when writing a function that takes a 2D array as a parameter, the second dimension must also be specified: void myFunc(char inArray[][5]) { /* body of function here… */ }

Notes on Project 5 Just like any other type, you can have 2D arrays of C-strings, e.g. char doc[50][81]; // 50 lines, 80 chars + ‘\0’ each Reading lines from istream is the same as from cin Here’s a way to read 50 lines from an istream& (say, ‘mystream’): int lines = 0; while (lines < 50 && mystream.getline(doc[lines++],80)) continue;

Variables Revisited Variables have a type, a name, and a value Arrays are extensions of this idea We can pass variables “by value” to functions, which causes their contents to be copied We can also pass “by reference”, which changes the original variable when the reference changes Arrays are always passed by reference…but why?

How Variables Are Stored int main() { int x; int y = 2; char c; double d; int q[5] = { 1, 2, 3 }; x = 3; c = ‘H’; d = 3.5; cout << “x: ” << x << endl; return 0; }

How Variables Are Stored int main() { int x; int y = 2; char c; double d; int q[5] = { 1, 2, 3 }; x = 3; c = ‘H’; d = 3.5; cout << “x: ” << x << endl; return 0; } 32‘H’3.5123?? xycdq[0]q[1]q[2]q[3]q[4] name: value:

Type Sizes (For 32-bit Systems) NameSizeRange char1 byte signed: -128 to 127 unsigned: 0 to 255 short int2 bytes to int4 bytes to bool1 bytetrue or false float4 bytes+/- 3.4e +/- 38 (~7 digits) double8 bytes+/- 1.7e +/- 308 (~15 digits)

Variable Storage, Scaled to Size 32H3.5 xycd name: value: 1 q[0] 2 q[1] 3 q[2] ? q[3] ? q[4] name: value: 4 bytes

One More Attribute for Variables In addition to a type, a name, and a value, variables also have an address Just like the address of a house, it specifies where to find that variable in memory – Arrays are kind of like apartments, where each slot specifies a different place, all starting from the same address to 32 …

Variables with Addresses 32H3.5 xycd name: value: 1 q[0] 2 q[1] 3 q[2] ? q[3] ? q[4] name: value: 4 bytes

Operations on Addresses From the previous example: – x has address 0, y has address 4, c has address 8, d is at 9 – q and has 21, 25, 29, and 33 Retrieve the address of a variable/array using the ‘&’ operator, e.g.: cout << (&x); // might print ‘0’ Get the value at an address using the ‘*’ operator, e.g.: cout << *(&x); // would print 3

Storing an Address, “Pointers” Even though addresses are just numbers, they are very often stored in a special family of types – called the “pointer types”; an instance of this type is a “pointer” For instance, the address of an int can be stored in a pointer type called int*, read as “pointer to int” – one pointer type per regular type, e.g. int has “int*”, char has “char*”, etc. – There’s a special pointer type called “void*”, but we’ll get to that later…

Creating and Assigning a Pointer Assuming the previous code, this creates a pointer to ‘x’ and manipulates it a bit: // stores address of x in px int* px = &x; // prints address of x cout << (int)(px) << endl; // changes x to 32 *(px) = 32; 32‘H’3.5 xycd name: value: 0 px …

Pointers to Arrays (and Pointer Arithmetic) There is no difference between a pointer to a variable and a pointer to an array – …but this is where the type of the pointer starts to become useful Set ‘pq’ to point to our ‘q’ array: int* pq = q; // q was already a pointer! // …q[0] is the value of the first element Move ‘pq’ to the next element in ‘q’: pq = pq + 1; // moves pq 4b forward ‘pq’ moves forward by 4 bytes because it’s a pointer to int, which is of size 4 – if it were a pointer to char, we’d move forward 1 byte and be in the middle of the first element rather than at the second element

Array Access Revisited As established, ‘q’ is actually a pointer that points to the first element in its array – This is why we’ve had to pass the length of an array around all the time, because all ‘q’ knows is that it starts at some place and has a type – ‘q’s value tells it where to start, and its type tells it how to get to a particular element’s offset (e.g. by multiplying size of type and desired element position, then adding it to the address of the first element, which is ‘q’) The operator q[x] is actually *(q + size of type * index), e.g.: // let’s get the third element, slot 2 cout << *(q + 4*2); // 4 == size of int in bytes // gets the value of the element // starting at q, then shifted over ‘x’ // units of the type’s size

Passing by Reference Revisited The ‘reference’ that’s passed to a function is actually the address of the variable C++ is doing you some confusing ‘favors’, like using ‘&’ on the argument and implicitly using ‘*’ every time the reference is mentioned – added to the language because passing pointers is so common

Passing by Reference, Part 2 This code: void p(int& x) { x = 32; } int main() { int a; p(a); cout << a; /*etc*/ } …is (almost) equivalent to this code: void p(int* x) { *x = 32; } int main() { int a; p(&a); cout << a; /*etc*/ }

Addresses and Pointers Review All variables have an address in memory (as well as an amount of space they consume dictated by their type) The address can be retrieved via the ‘&’ operator The value at an address can be retrieved/changed using the ‘*’ operator on an address

Addresses and Pointers Review, Part 2 A pointer is a special type used for holding addresses Pointers can be manipulated using +, -, which moves them in ‘steps’ of their type’s size, e.g.: int* p; p = p + 3; // moves p (3*4) = 12 bytes forward in memory Arrays are actually pointers to the first element – The subscript operator [] is implemented using pointer arithmetic; assuming q is an array of ints: q[x] == *(q+4*x)