Presentation is loading. Please wait.

Presentation is loading. Please wait.

References and Pointers

Similar presentations


Presentation on theme: "References and Pointers"— Presentation transcript:

1 References and Pointers
CSCE 121 Modified from slides created by Carlos Soto

2 You’ve already seen some datatypes:
int long long float double char string *you’ll usually want to stick to the ones in bold If you’ve been keeping up with your reading..

3 We can create a variable in our code by specifying a datatype and a variable identifier or name. For example: int count; string name;

4 We can create a variable in our code by specifying a datatype and a variable identifier or name. For example: int count; string name; Datatype is “int”

5 We can also initialize our variables
We can also initialize our variables. For example: int count = 1; string name = "Adam";

6 Our variables hold values, and we can use those values directly
Our variables hold values, and we can use those values directly. For example: int total = count + 10; cout << "My name is " << name;

7 We can also access values indirectly with two important programming language constructs:
Pointers and References Pointers and references are complex topics, and we are only covering them lightly for now. It may not seem apparent yet why they are useful, but they become very important later on.

8 Pointers are variables whose value is the address in memory of another variable. References are special variables that act as aliases to another variable. (But also have a value that is the address in memory of another variable.) So the value of a pointer is always a memory address. That is, a number. Making sure that this number is a valid address that your program has access to becomes important later for programming safety. References are basically nick-names (or second names) for variables you already have in your program, so you can use them just like you’d use the original variable. The difference is that references have the special property that references work across diference scopes, as we’ll see later. I’m going to show you guys what it looks like to use pointers and references, but don’t worry too much about them for now.

9 Using pointers: int count = 1; int. countPtr = &count; int
Using pointers: int count = 1; int *countPtr = &count; int *countPtr2 = countPtr; cout << *countPtr; This would also work for char, string, etc. countPtr and countPtr2 are both storing the address of count.

10 Using pointers: int count = 1; int *countPtr = &count;
Datatype is “pointer to an int”

11 Using pointers: int count = 1; int* countPtr = &count;
This is equivalent, so the * can go with the datatype or with the identifier. Datatype is “pointer to an int”

12 Using pointers: int count = 1; int. countPtr = &count; int
Using pointers: int count = 1; int *countPtr = &count; int *countPtr2 = countPtr; cout << *countPtr; “address of” operator If we instead assigned the value of count, we’d have an invalid address.

13 Using pointers: int count = 1; int. countPtr = &count; cout <<
Using pointers: int count = 1; int *countPtr = &count; cout << *countPtr; “dereferences” the pointer What would happen if we printed countPtr without dereferencing.

14 Pointers are considered dangerous because it is easy to lose track of the addresses you’re pointing to, and try to dereference memory you don’t “own”. That’s a big no no. So C++ also has References, which some people think of as safer pointers.

15 Using references int count = 1; int& countRef = count; int& countRef2 = count; int& countRef2 = countRef; cout << countRef;

16 Using references int count = 1; int& countRef = count;
Datatype is “reference to an int”

17 Using references int count = 1; int& countRef = count; cout << countRef;
No “address of” needed No dereferencing needed

18 Takeaway messages for now:
C++ has two special datatypes called Pointers and References. They let you access values indirectly. You need to at least know they exist for now References are safer, usually better than pointers The symbols you’ll see when using pointers and references are: & and *


Download ppt "References and Pointers"

Similar presentations


Ads by Google