0% found this document useful (0 votes)
26 views13 pages

Informatics 2 - 2,3

Uploaded by

wskwarek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views13 pages

Informatics 2 - 2,3

Uploaded by

wskwarek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Lesson 2, 3

Pointers and references.

Wskaźniki i referencje
The Agenda
1. Memory allocation concept
2. Reference oparator
3. Dereference operator
4. Comparing reference and dereference operators
5. Declaring variables of pointer types
6. Dynamic memory allocation
7. Examples
Terms
The memory of your computer can be imagined as a succession of memory
cells, each one of the minimal size that computers manage (one byte). These
single-byte memory cells are numbered in a consecutive way, so as, within any
block of memory, every cell has the same number as the previous one plus
one.

This way, each cell can be easily located in the memory because it has a unique
address and all the memory cells follow a successive pattern.
Reference operator (&)
The address that locates a variable within memory is what we call a reference
to that variable.

This reference to a variable can be obtained by preceding the identifier of a


variable with an ampersand sign (&), known as reference operator, and which
can be literally translated as "address of".
Reference operator (&)
Example 1:

Program:
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int a = 10; // local variable
5. cout << "a = " << a << endl;
6. cout << "a address = " << &a << endl;
7. return 0;
8. }

OUTPUT:
a = 10
a address = 0x6efe1c
Dereference operator (*)
We have just seen that a variable which stores a reference to another variable
is called a pointer. Pointers are said to "point to" the variable whose reference
they store.
Using a pointer we can directly access the value stored in the variable which it
points to. To do this, we simply have to precede the pointer's identifier with an
asterisk (*), which acts as dereference operator and that can be literally
translated to "value pointed by".
Comparing * and & operators
Difference between the reference and dereference operators:

● & is the reference operator and can be read as "address of"

● * is the dereference operator and can be read as "value pointed by"

Thus, they have opposite meanings.

A variable referenced with & can be dereferenced with *.


Declaring variables of pointer types
The declaration of pointers follows this format:
1. type * name;

This type is not the type of the pointer itself! but the type of the data the
pointer points to. For example:
1. int * number;
2. char * character;
3. float * greatnumber;
Declaring variables of pointer types
Example:
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int firstvalue, secondvalue;
5. int * mypionter;
6. mypointer = &firstvalue;
7. *mypointer = 10;
8. mypointer = &secondvalue;
9. *mypointer = 20;
10. cout << "firstvalue is " << firstvalue << endl;
11. cout << "secondvalue is " << secondvalue << endl;
12. return 0;
13.}
OUTPUT:
firstvalue is 10
secondvalue is 20
Dynamic memory allocation
Dynamic memory allocation in C++ allows you to allocate memory during the
runtime of a program.
This is useful when you don't know the required size of memory at compile
time or when you need to manage memory manually for data structures like
arrays, linked lists, or when dealing with large amounts of data.
C++ provides two primary operators for dynamic memory allocation: new and
delete.
To allocate memory for a single element, use the new operator followed by
the data type of the element.
int * ptr = new int; //Allocates memory for a single integer
After you're done using dynamically allocated memory, it's essential to release
it to avoid memory leaks. For a single element, use the delete operator
followed by the pointer variable.
delete ptr; // Deallocates memory for the single integer
// pointed by ptr
Dynamic memory allocation
Example
1. #include <iostream>
2. using namespace std;
3. int main() {
4. // Dynamic memory allocation for a single integer variable
5. int* ptr = new int;
6. // Check if memory allocation was successful
7. if (ptr != nullptr) {
8. // Input value for the dynamically allocated integer
9. cout << "Enter an integer value: ";
10. cin >> *ptr;
11. // Output the value stored in the dynamically allocated integer
12. cout << "Value entered: " << *ptr << endl;
13. // Don't forget to deallocate the dynamically allocated memory
14. delete ptr;
15. } else {
16. cout << "Memory allocation failed!" << endl;
17. }
18. return 0;
19. }
Problem
Write a C++ program that uses pointers to perform some basic operations on variables.

1. Declare two integer variables: num1 and num2.


2. Declare two pointers to integers: ptr1 and ptr2.
3. Assign values to num1 and num2 (you can take input from the user or use fixed
values).
4. Assign the addresses of num1 and num2 to the pointers ptr1 and ptr2, respectively.
5. Perform the following operations using pointers:
a) Add the values of num1 and num2 using pointers and store the result in a new
integer variable sum.
b) Subtract the value of num2 from num1 using pointers and store the result in a
new integer variable difference.
c) Multiply the values of num1 and num2 using pointers and store the result in a
new integer variable product.
d) Divide the value of num1 by num2 using pointers and store the result in a new
float variable quotient.
6. Print the values of sum, difference, product, and quotient.
Homework
Write a C++ program that demonstrates the use of pointers to swap two
numbers.

1. Declare two integer variables: a and b.


2. Assign values to a and b (you can take input from the user or use fixed
values).
3. Declare two pointers to integers: ptrA and ptrB.
4. Assign the addresses of a and b to the pointers ptrA and ptrB, respectively.
5. Use pointers to swap the values of a and b without using any temporary
variables.
6. Print the values of a and b after the swap.

You might also like