CS31 Discussion 1H Fall18: week 7

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

Pointers in C Rohit Khokher
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
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.
Starting Out with C++, 3 rd Edition 1 Chapter 9 – Pointers.
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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
What are Pointers? Different from other normal variables which can store values. pointers are special variables that can hold the address of a variable.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Dynamic memory allocation and Pointers Lecture 4.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
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:
CS102 Introduction to Computer Programming Chapter 9 Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointers *, &, array similarities, functions, sizeof.
1 CS161 Introduction to Computer Science Topic #8.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Variables and memory addresses
1 2/2/05CS250 Introduction to Computer Science II Pointers.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
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.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
Intro to Pointers in C CSSE 332 Operating Systems
EGR 2261 Unit 11 Pointers and Dynamic Variables
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
Chapter 7 Pointers and C-Strings
UNIT 5 C Pointers.
Introduction to Programming
Documentation Need to have documentation in all programs
Learning Objectives Pointers Pointer in function call
Pointers and Pointer-Based Strings
New Structure Recall “average.cpp” program
Student Book An Introduction
COMP 2710 Software Construction Pointers
Visit for more Learning Resources
CNG 140 C Programming (Lecture set 10)
Lecture 6 C++ Programming
void Pointers Lesson xx
Buy book Online -
Chapter 15 Pointers, Dynamic Data, and Reference Types
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Pointers, Dynamic Data, and Reference Types
Given the code to the left:
CSC212 Data Structure - Section FG
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Initializing variables
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Arrays an array of 5 ints is filled with 3,2,4,1,7
Overloading functions
C++ Pointers and Strings
Pointers and Pointer-Based Strings
POINTER CONCEPT 4/15/2019.
Data Structures and Algorithms Introduction to Pointers
Pointers and dynamic objects
CS31 Discussion 1H Fall18: week 6
CS31 Discussion 1D Fall18: week 2
Pointers and dynamic objects
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
POINTER CONCEPT 8/3/2019.
Presentation transcript:

CS31 Discussion 1H Fall18: week 7 TA: Behnam Shahbazi bshahbazi@cs.ucla.edu Credit to former TAs: Sepideh Mazrouee

Pointers Every variable in C++ can be described by five different attributes: int main(void) { int bankAccount = 1000324; ... }   1. every variable has a name 2. every variable has a type 3. every variable has a size 4. every variable has a value 5. every variable has an address!   Acknowledgement: Professor Nachenberg – for the slides CS31 - Spring13 - Section1J TA: Sepid Maz

CS31 - Spring13 - Section1J TA: Sepid Maz Variable Addresses 00000000 00000001 Every byte in the computer‘s memory has its own address (just like each house on a street has an address). When you define a variable in your program, the computer finds an unused set of address in memory and reserves them for your variable. int main(void) { int stdID = 1500234; char grade = ‘B’; } … 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 stdID 1500234 grade 66 … 99999990 99999991 99999992 1 byte CS31 - Spring13 - Section1J TA: Sepid Maz

CS31 - Spring13 - Section1J TA: Sepid Maz Variable Addresses 00000000 00000001 Important: The address of a variable is defined to be the first address in memory where the variable is stored. Questions: 1. What is stdID’s address in memory? 2. What about grade’s address? int main(void) { int stdID = 1500234; char grade = ‘B’; } … 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 stdID 1500234 grade 66 … 99999990 99999991 99999992 CS31 - Spring13 - Section1J TA: Sepid Maz

Finding the Address of a Variable We can get the address of a variable using the & operator. int main(void) { int stdID = 1500234; char grade= ‘B’; cout << “Value: “ << stdID << endl; cout << “Size: “ << sizeof(stdID) << endl; cout << “StdID’s address: “<< &stdID<<endl; … cout << “grade’s address: “ << &grade; } If you place an & before a variable in your program, it means “give me the numerical address of the variable.” 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 stdID 1500234 grade 66 CS31 - Spring13 - Section1J TA: Sepid Maz

& is for References and Pointers When we place an & before a function parameter, we make the parameter a reference parameter.   void booboo(int &a) { ... // a is a reference parameter. } When we place an & before a variable in a program statement, we are asking for that variable’s address. int main(void) int a;   cout << &a; // get a’s address ... }  Be careful not to confuse the two. CS31 - Spring13 - Section1J TA: Sepid Maz

Every Variable Has An Address ... 00001062 Question: What is the output for this code? void change_me(int x);   int main(void) { int n; cout << "Enter a #: "; cin >> n; cout << n << endl; change_me(n); } // definition void change_me(int x) x = 12; n 3 x 12 3 00001070 ... Output: Enter a #: 3 3 3 CS31 - Spring13 - Section1J TA: Sepid Maz

CS31 - Spring13 - Section1J TA: Sepid Maz The Pointer Variable A pointer variable is a variable that holds an address value rather than a normal int, float, double or string value.   You can set a pointer variable equal to a variable’s address: int main(void) { float price = 300; Pointer ptr; // a pointer variable ptr = &price; // ptr gets price’s address ... On PCs, pointer variables require 4 bytes of storage. Why? (depending on the machine- it’s 4 bytes for 32bit system) ... 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 price 300 ptr 1000 CS31 - Spring13 - Section1J TA: Sepid Maz

The sizeof a Pointer Variable A pointer variable needs to be able to point to any address in the computer’s memory. Let say a 32 bit system that has 4 gigabytes (4 billion bytes) of memory. Therefore, each pointer variable needs to be able to hold values between 0 and 4,000,000,000 (4 billion). In order for a pointer variable to hold values between 0 and 4 billion, it needs 4 bytes of storage (232 values). CS31 - Spring13 - Section1J TA: Sepid Maz

Defining a Pointer Variable… int main(void) { float price = 300; Pointer ptr; // WRONG!!! Incorrect syntax ptr = &price; // ptr gets price’s address When you define a pointer variable, you must specify what type of variable the pointer will point to… CS31 - Spring13 - Section1J TA: Sepid Maz

How to Define a Pointer Variable… int main(void) { float price = 300; float * ptr; // ptr can point to float variables ptr = &price; // ptr gets price’s address To define a pointer variable, you use the following syntax: Type * pointer_name; e.g. int * anIntPointer, *anotherOne; string *ptr_to_a_string; When you define your variables the * indicates that the variable is a pointer and not a regular variable.

Assigning a Pointer Variable… The type of the pointer variable must match the type of the variable it’s pointing to. main() { float *fptr; ...  fptr may only point to float variables.   int *pToInt; … pToInt may only point to integer variables. double dip; // dip is a double variable fptr = &dip; // ERROR! fptr can only point to a float variable int stdID, courseNo; // stdID and courseNo are int variables pToInt = &stdID; // This is just fine… pToInt = &courseNo; // This too… Now pToInt points to courseNo CS31 - Spring13 - Section1J TA: Sepid Maz

Assigning a Pointer Variable… 00000000 00000001 int main(void) { int stdID, courseNo; stdID = 70232143; courseNo = 150012; int *ptr; cout << “stdID is at address “ << &stdID << endl; ptr = &courseNo; cout << “ptr points to address “ << ptr << endl; }   … stdID 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 70232143 courseNo 150012 ptr 1004 ? … courseNo is at address 1004 ptr points to address 1004 CS31 - Spring13 - Section1J TA: Sepid Maz

Assigning a Pointer Variable… int main(void) { int stdID, courseNo; stdID = 70232143; courseNo = 150012; int *ptr; cout << “stdID is at address “ << & stdID << endl; ptr = & stdID; cout << “ptr points to address “ << ptr << endl; ptr = & courseNo; } 00000000 00000001 … stdID 00001000 00001001 00001002 00001003 00001004 00001005 00001006 00001007 00001008 00001009 00001010 00001011 70232143 courseNo 150012 ptr 1000 1004 … stdID is at address 1000 ptr points to address 1000 CS31 - Spring13 - Section1J TA: Sepid Maz ptr points to address 1004

Pointer Variables are Variables Too! A pointer variable, like a regular variable has the same 5 attributes. 1. name: pfv 2. type: pointer to a float variable 3. size: 4 bytes (= 32bits) 4. value: the value of a pointer variable is the address of another variable. 5. an address: Pointer variables have their OWN address too! main() { float fv = 3.14; float *pfv;   pfv = &fv; cout << “value: “ << pfv << endl; cout << “size: "<<sizeof(pfv)<<endl; cout << "address: " << &pfv << endl; } CS31 - Spring13 - Section1J TA: Sepid Maz

Pointer Variable Attributes 00000000 00000001 main() { float fv = 3.14; float *pfv;   pfv = &fv; cout << “value: “ << pfv << endl; cout << “size: "<<sizeof(pfv)<<endl; cout << "address: " << &pfv << endl; } … 00009240 00009241 00009242 00009243 00009245 00009246 00009247 00009248 00009249 00009250 00009251 fv 3.14 pfv 9241 value: 9241 size: 4 … address: 9245 CS31 - Spring13 - Section1J TA: Sepid Maz

CS31 - Spring13 - Section1J TA: Sepid Maz Pointer Challenge Question: what is the size of the following variables? 00000000 00000001 main() { char c = ‘A’; short s = 42;   char *pc = &c; short *ps = &s; bool *pbool; ... } … c 65 00009240 00009241 00009242 00009243 00009244 00009245 00009246 00009247 00009248 00009249 00009250 00009251 s 42 pc 9240 ps 9241 All pointer variables are 4 bytes long, regardless of what type of variable they point to, since they have to hold an address. CS31 - Spring13 - Section1J TA: Sepid Maz …

CS31 - Spring13 - Section1J TA: Sepid Maz * Operator The * operator is used to “de-reference” a pointer. It lets you access what the pointer points to. main() { short hands = 2; short *ptr; ptr = &hands; *ptr = 3; cout << *ptr << endl; cout << hands << endl; } 00000000 00000001 … hands 2 00009240 00009241 00009242 00009243 00009244 00009245 00009246 00009247 00009248 00009249 00009250 3 ptr 9240 3 3 “Store a value of 3 where ptr points to.” “Print the value stored where ptr points.” CS31 - Spring13 - Section1J TA: Sepid Maz …

Question: What Happens Here? 00000000 00000001 main() { int *iptr;   *iptr = 123456; } … iptr 00009240 00009241 00009242 00009243 00009244 00009245 00009246 00009247 00009248 00009249 00009250 989699 Where does iptr point to? It points randomly in memory! You must always point a pointer variable at another variable before using the * operator on it! 123456 CS31 - Spring13 - Section1J TA: Sepid Maz Operating system??

CS31 - Spring13 - Section1J TA: Sepid Maz Another Example… 00000000 00000001 main() { int a=1, b=2; int *p1, *p2;  p1 = &a; p2 = &b;   *p1 = *p2; } … a 1 b 2 00006420 00006421 00006422 00006423 00006424 00006425 00006426 00006427 00006428 00006429 00006430 00006431 00006432 00006433 00006434 00006435 2 p1 p2 6420 “Get the value pointed to by p2. Store this value in the variable pointed to by p1.” 6424 CS31 - Spring13 - Section1J TA: Sepid Maz …

CS31 - Spring13 - Section1J TA: Sepid Maz Another Example… 00000000 00000001 main() { int a=1, b=2; int *p1, *p2; // how to define 2 ptrs   p1 = &a; p2 = &b; p1 = p2; } … a 2 b p1 p2 00006420 00006421 00006422 00006423 00006424 00006425 00006426 00006427 00006428 00006429 00006430 00006431 00006432 00006433 00006434 00006435 6420 6424 6424 “Store the value of variable p2 into variable p1.” CS31 - Spring13 - Section1J TA: Sepid Maz …

CS31 - Spring13 - Section1J TA: Sepid Maz Question: What do you think about this example? 00000000 00000001 main() { int a=1, b=2; int *p1, *p2; // how to define 2 ptrs   p1 = &a; p2 = &b; if (*p1 == *p2) { do something; } } … 00006420 00006421 00006422 00006423 00006424 00006425 00006426 00006427 00006428 00006429 00006430 00006431 00006432 00006433 00006434 00006435 a 1 b 2 p1 p2 6420 6424 “Compare the value pointed to by p1 to the value pointed to by p2.” if (1 == 2) … CS31 - Spring13 - Section1J TA: Sepid Maz …

CS31 - Spring13 - Section1J TA: Sepid Maz Question: How about this? 00000000 00000001 main() { int a=1, b=2; int *p1, *p2; // how to define 2 ptrs   p1 = &a; p2 = &b; if (p1 == p2) { do something; } } … a 1 b 2 p1 p2 00006420 00006421 00006422 00006423 00006424 00006425 00006426 00006427 00006428 00006429 00006430 00006431 00006432 00006433 00006434 00006435 6420 6424 “Compare the value of variable p1 to the value of variable p2.” if (6420 == 6424) … CS31 - Spring13 - Section1J TA: Sepid Maz …

Using Pointers Instead of References 00000000 00000001 void set(int *pa) // like a reference! { pa = 5; }   main() int x = 1; set(&x); cout << &x; … x 1 00009240 00009241 00009242 00009243 00009244 00009245 00009246 00009247 00009248 00009249 00009250 5 pa 9240 Store a value of 5 in the variable where pa points (at location 9240). CS31 - Spring13 - Section1J TA: Sepid Maz

CS31 - Spring13 - Section1J TA: Sepid Maz Question: what is the output of this code? 00000000 00000001 void set(int *pa) // like a reference! { *pa = 5; }   main() int x = 1; set(&x); cout << x; … x 5 00009240 00009241 00009242 00009243 00009244 00009245 00009246 00009247 00009248 00009249 00009250 5 CS31 - Spring13 - Section1J TA: Sepid Maz

References to Pointers short arr[3] = {2,-3, 1}; // global void dosth(short * & r2p) { r2p = &arr[1]; }   main() short *ptr = NULL; dosth(ptr); cout << *ptr; 00000000 00000001 … arr 00009240 00009241 00009242 00009243 00009244 00009245 00009246 00009247 00009248 00009249 00009250 00009251 00009252 00009253 // ptr = &arr[1]; 2 -3 1 ptr 9242 NULL r2p is a reference to a short pointer variable. r2p So any time we refer to r2p, we’re really referring to ptr! CS31 - Spring13 - Section1J TA: Sepid Maz

What If We Drop the Reference? short arr[3] = {2,-3, 1}; // global void dosth(short * & r2p) { r2p = &arr[1]; }   main() short *ptr = NULL; dosth(ptr); cout << *ptr; 00000000 00000001 … arr 00009240 00009241 00009242 00009243 00009244 00009245 00009246 00009247 00009248 00009249 00009250 00009251 00009252 00009253 2 -3 1 NULL ptr NULL r2p NULL 9242 r2p is a regular short pointer var. It gets a copy of ptr’s value. So when we refer to r2p, we’re referring to a copy of ptr!

CS31 - Spring13 - Section1J TA: Sepid Maz Question: What does it print? main() { char ch = 'A'; char *pch,*pch2;   pch = &ch; pch2 = pch; (*pch2)++; cout << ch; } 00000000 00000001 … x 65 66 00002200 00002201 00002202 00002203 00002204 00002205 00002206 00002207 00002208 00002209 00002210 pch pch2 2200 2200 “Increment the value pointed to by pch2.” B CS31 - Spring13 - Section1J TA: Sepid Maz

CS31 - Spring13 - Section1J TA: Sepid Maz Review A pointer is a memory address of a variable. We have already used pointer concepts through the use of Call-by-reference Passing an array identifiers through functions QUESTION: Using x as an integer and p as an int pointer, you should be able to know what's being accessed in each of these cases: x: &x: *p: p: &p: value of x address of x the value stored at the address that p refers to address that p refers to (value of p) address of p (p is a variable, and all variables have addresses. CS31 - Spring13 - Section1J TA: Sepid Maz

CS31 - Spring13 - Section1J TA: Sepid Maz int *p1; *p1 = 100; cout << *p1 << endl; Output? int *p1; int v1; v1 = -7; p1 = &v1; *p1 = 100; cout << v1 << endl; cout << *p1 << endl; Output? CS31 - Spring13 - Section1J TA: Sepid Maz

CS31 - Spring13 - Section1J TA: Sepid Maz Additional Materials Check my webpage for additional materials. Behnam CS31 - Spring13 - Section1J TA: Sepid Maz