Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS31 Discussion 1H Fall18: week 7

Similar presentations


Presentation on theme: "CS31 Discussion 1H Fall18: week 7"— Presentation transcript:

1 CS31 Discussion 1H Fall18: week 7
TA: Behnam Shahbazi Credit to former TAs: Sepideh Mazrouee

2 Pointers Every variable in C++ can be described by five different attributes: int main(void) { int bankAccount = ; ... }   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

3 CS31 - Spring13 - Section1J TA: Sepid Maz
Variable Addresses 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 = ; char grade = ‘B’; } stdID grade 66 1 byte CS31 - Spring13 - Section1J TA: Sepid Maz

4 CS31 - Spring13 - Section1J TA: Sepid Maz
Variable Addresses 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 = ; char grade = ‘B’; } stdID grade 66 CS31 - Spring13 - Section1J TA: Sepid Maz

5 Finding the Address of a Variable
We can get the address of a variable using the & operator. int main(void) { int stdID = ; 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.” stdID grade 66 CS31 - Spring13 - Section1J TA: Sepid Maz

6 & 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

7 Every Variable Has An Address
... 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 ... Output: Enter a #: 3 3 3 CS31 - Spring13 - Section1J TA: Sepid Maz

8 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) ... price 300 ptr 1000 CS31 - Spring13 - Section1J TA: Sepid Maz

9 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

10 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

11 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.

12 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

13 Assigning a Pointer Variable…
int main(void) { int stdID, courseNo; stdID = ; courseNo = ; int *ptr; cout << “stdID is at address “ << &stdID << endl; ptr = &courseNo; cout << “ptr points to address “ << ptr << endl; } stdID courseNo 150012 ptr 1004 ? courseNo is at address 1004 ptr points to address 1004 CS31 - Spring13 - Section1J TA: Sepid Maz

14 Assigning a Pointer Variable…
int main(void) { int stdID, courseNo; stdID = ; courseNo = ; int *ptr; cout << “stdID is at address “ << & stdID << endl; ptr = & stdID; cout << “ptr points to address “ << ptr << endl; ptr = & courseNo; } stdID 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

15 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

16 Pointer Variable Attributes
main() { float fv = 3.14; float *pfv; pfv = &fv; cout << “value: “ << pfv << endl; cout << “size: "<<sizeof(pfv)<<endl; cout << "address: " << &pfv << endl; } fv 3.14 pfv 9241 value: 9241 size: 4 address: 9245 CS31 - Spring13 - Section1J TA: Sepid Maz

17 CS31 - Spring13 - Section1J TA: Sepid Maz
Pointer Challenge Question: what is the size of the following variables? main() { char c = ‘A’; short s = 42; char *pc = &c; short *ps = &s; bool *pbool; ... } c 65 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

18 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; } hands 2 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

19 Question: What Happens Here?
main() { int *iptr; *iptr = ; } iptr 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??

20 CS31 - Spring13 - Section1J TA: Sepid Maz
Another Example… main() { int a=1, b=2; int *p1, *p2;  p1 = &a; p2 = &b; *p1 = *p2; } a 1 b 2 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

21 CS31 - Spring13 - Section1J TA: Sepid Maz
Another Example… 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 6420 6424 6424 “Store the value of variable p2 into variable p1.” CS31 - Spring13 - Section1J TA: Sepid Maz

22 CS31 - Spring13 - Section1J TA: Sepid Maz
Question: What do you think about this example? 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 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

23 CS31 - Spring13 - Section1J TA: Sepid Maz
Question: How about this? 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 6420 6424 “Compare the value of variable p1 to the value of variable p2.” if (6420 == 6424) CS31 - Spring13 - Section1J TA: Sepid Maz

24 Using Pointers Instead of References
void set(int *pa) // like a reference! { pa = 5; } main() int x = 1; set(&x); cout << &x; x 1 5 pa 9240 Store a value of 5 in the variable where pa points (at location 9240). CS31 - Spring13 - Section1J TA: Sepid Maz

25 CS31 - Spring13 - Section1J TA: Sepid Maz
Question: what is the output of this code? void set(int *pa) // like a reference! { *pa = 5; } main() int x = 1; set(&x); cout << x; x 5 5 CS31 - Spring13 - Section1J TA: Sepid Maz

26 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; arr // 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

27 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; arr 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!

28 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; } x 65 66 pch pch2 2200 2200 “Increment the value pointed to by pch2.” B CS31 - Spring13 - Section1J TA: Sepid Maz

29 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

30 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

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


Download ppt "CS31 Discussion 1H Fall18: week 7"

Similar presentations


Ads by Google