[go: up one dir, main page]

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

Lec 04 Pointers

c++

Uploaded by

wwerazo8
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 views27 pages

Lec 04 Pointers

c++

Uploaded by

wwerazo8
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/ 27

Computer Language II

Lecture 4: Pointers

Dr. Khari Armih

khari.armih@gmail.com

June 9, 2023

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 1 / 27


Outline
❶ Pointer
❷ Arrays and pointers
❸ Passing array using pointer

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 2 / 27


Memory Basics – Regular Variables
• All variables have two parts:
– value
– address where value is stored

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 3 / 27


Memory Basics – Regular Variables
• So the code to declare is:

int x = 3;

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 4 / 27


Pointers
type *name
• Pointer is a variable
• Contains a memory address
• Points to specific data type;

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 5 / 27


Memory Basics – Regular Variables
• We can also declare a pointer:

int *ptr;

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 6 / 27


Memory Basics – Regular Variables
• and set it equal to the address of x:

int x = 3;

int *ptr;

ptr = &x;

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 7 / 27


Pointers
• We can have pointer to any data type
• Example:
int *xPtr;
float *yPtr;
• The * can be anywhere between the type and the variable
• Example:
int * xPtr;
float * yPtr;

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 8 / 27


Pointers (cont.)
• You can assign the address of a variable to a compatible pointer
using & operator
• Example:

int num;
int * numPtr;
numPtr = &num
• You can print the address stored in a pointer using:

cout << numPtr;

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 9 / 27


The * Operator
• The * operator allows pointers to access variable points to.
• Example:

char c = ’A’;
char *cPtr;
cPtr = &c;
*cPtr = ’B’;

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 10 / 27


Full Example
1 #include<iostream>
2 using namespace std;
3 int main()
4 {
5 int *p;
6 int num1 = 5;
7 int num2 = 8;
8 //store the address of num1 into p
9 p = &num1;
10 cout << "Line 10: &num1 = " << &num1 << ", p = " << p << endl;
11 cout << "Line 11: num1 = " << num1 << ", *p = " << *p << endl;
12 *p = 10;
13 cout << "Line 13: num1 = " << num1 << ", *p = " << *p << endl << endl;
14 //store the address of num2 into p
15 p = &num2;
16 cout << "Line 16: &num2 = " << &num2 << ", p = " << p << endl;
17 cout << "Line 17: num2 = " << num2 << ", *p = " << *p << endl;
18 *p = 2 * (*p);
19 cout << "Line 19: num2 = " << num2 << ", *p = " << *p << endl;
20 return 0;
21 }

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 11 / 27


Sample run
Line 10: &num1 = 0x6ffe34, p = 0x6ffe34
Line 11: num1 = 5, *p = 5
Line 13: num1 = 10, *p = 10
Line 16: &num2 = 0x6ffe30, p = 0x6ffe30
Line 17: num2 = 8, *p = 8
Line 19: num2 = 16, *p = 16

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 12 / 27


Example: Swap 1
swa1p.cpp
1 #include <iostream>
2 using namespace std;
3 void swap(int a, int b)
4 {
5 int t = a;
6 a = b;
7 b = t;
8 cout << "Inside: x = " << a <<" , "<< "y = "<< b << endl;
9 }
10
11 int main()
12 {
13 int x, y;
14 x = 1;
15 y = 2;
16 cout << "Before: x = " << x <<" , "<< "y = "<< y << endl;
17 swap(x,y);
18 cout << " After: x = " << x <<" , "<< "y = "<< y << endl;
19 return 0;
20 }

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 13 / 27


Sample run
Before: x = 1 , y = 2
Inside: x = 2 , y = 1
After: x = 1 , y = 2

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 14 / 27


Example: Swap 2
swap2.cpp
1 #include <iostream>
2 using namespace std;
3 void swap(int *a, int *b)
4 {
5 int t = *a;
6 *a = *b;
7 *b = t;
8 cout << "Inside: x = " << *a <<","<< "y = "<< *b << endl;
9 }
10
11 int main()
12 {
13 int x, y;
14 x = 1;
15 y = 2;
16 cout << "Before: x = " << x <<","<< "y = "<< y << endl;
17 swap(&x,&y);
18 cout << "After: x = " << x <<","<< "y = "<< y << endl;
19 return 0;
20 }

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 15 / 27


Sample run
Before: x = 1 , y = 2
Inside: x = 2 , y = 1
After: x = 2 , y = 1

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 16 / 27


Why Pointers
• The are two main reasons for using pointers:
• Efficiency
– Pointers are typically represented by one machine word
– Storing pointers instead of copies of large objects safes memories
– Passing pointers instead of large objects is much more efficient
• Dynamically growing data structures
– Each data type has a fixed size and memory layout
– Pointers allow us to build dynamically growing data structures by
adding and removing fixed size cells
• Other languages use pointers heavily – you just don’t see them!

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 17 / 27


Arrays and Pointers
• Primitive arrays implemented using pointer to block of
contiguous memory
• Consider array of 5 ints:

int arr[5];
• arr is like a pointer to element 0 of the array:

int *p = arr; == int *p = &arr[0];

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 18 / 27


Arrays and Pointers Cont.
• Arrays are implicitly managed using pointers and pointer
arithmetics
• Given an index, the compiler calculates an address offset that now
points to the proper array element
• It will not check whether this points outside of the array

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 19 / 27


Arrays and Pointers Cont.

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 20 / 27


Passing Arrays as Arguments
• Passing arrays by reference
– only the address of the first element of the array is passed
• Example:

int arr[10];
• can be passed to a function as follows:

void func(int array[], int size);


• call func as:

func(arr,10);

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 21 / 27


Example: Average 1
1 #include<iostream>
2 using namespace std;
3 float getAverage(int arr[], int size)
4 {
5 float ave, sum=0;
6 int i=0;
7 while(i<size)
8 {
9 sum+=arr[i];
10 i++;
11 }
12 ave = sum/size;
13 return ave;
14 }
15 int main()
16 {
17 int a[5] = {1,2,3,4,5};
18 // pass pointer to the array as an argument
19 float average = getAverage(a,5);
20 cout << "Average = " << average << endl;
21 return 0;
22 }
Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 22 / 27
Passing Arrays as Arguments
• passing arrays to functions

float getAverage(int arr[], int size)


float getAverage(int *arr, int size)

• both of these behave the same way


– they take a pointer to the beginning of an array

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 23 / 27


Example: Average 2
1 #include<iostream>
2 using namespace std;
3 float getAverage(int *arr, int size)
4 {
5 float ave, sum=0;
6 int i=0;
7 while(i<size)
8 {
9 sum+=arr[i];
10 i++;
11 }
12 ave = sum/size;
13 return ave;
14 }
15 int main()
16 {
17 int a[5] = {1,2,3,4,5};
18 // pass pointer to the array as an argument
19 float average = getAverage(a,5);
20 cout << "Average = " << average << endl;
21 return 0;
22 }
Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 24 / 27
In-Class Exercise
1 #include <iostream>
2 using namespace std;
3

4 void findSize(int arr[])


5 {
6 cout << sizeof(arr) << endl;
7 }
8

9 int main()
10 {
11 int a[100];
12 cout << sizeof(a) << " ";
13 findSize(a);
14 return 0;
15 }

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 25 / 27


Output
400
8

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 26 / 27


lab 4
• Write a C++ function to find the k largest elements in a given
array of integers.
• Write C++ function to find specific value in the array.
• Write a C++ function to find the most frequent element in an array
of integers.
• Write a C++ program to separate even and odd numbers in an
array of integers. Put all even numbers first, and then odd
numbers

Dr. Khari Armih (khari.armih@gmail.com) Computer Language II June 9, 2023 27 / 27

You might also like