Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx

ab11167 11 views 16 slides Aug 27, 2024
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

### Pointers in Programming: An Overview

#### Introduction

Pointers are a fundamental concept in programming, particularly in languages like C, C++, and others that provide low-level memory management capabilities. A pointer is a variable that stores the memory address of another variable. This mi...


Slide Content

ENGR-UH 1000 Lab 8 Pointers

Pointers in C++ Addresses in memory Programs can manipulate addresses directly & expr Evaluates to the address of the location expr evaluates to * expr Evaluates to the value stored in the address expr evaluates to

Parameter Passing in C++ Actual parameters are values void swap (int a, int b) { int tmp = b; b = a; a = tmp; } int main (void) { int i = 3; int j = 4; swap (i, j); … } The value of i (3) is passed, not its location! swap does nothing

Parameter Passing Using Pointers void swap (int *a, int *b) { int tmp = *b; *b = *a; *a = tmp; } int main (void) { int i = 3; int j = 4; swap (&i, &j); cout << i << j << endl; } The value of &i is passed, which is the address of i

Pointers and Arrays The name of the array variable is a pointer to the first element of that array! int arr[5] = {1, 2, 3, 4, 5}; // initialize array int *ptr = arr; // set pointer to array // You can now use ptr and arr interchangeably! cout << ptr[2] << ‘\n’; // What will this print?

Pointers and Arrays The same relationship goes backwards as well! int arr[5] = {1, 2, 3, 4, 5}; // initialize array cout << arr << ‘\n’; // what will this print? // in fact, when we use square brackets, we actually do the following: cout << arr[3] << ‘\n’; // this is the same thing as: cout << *(arr + 3) << ‘\n’; // this works because array variable names are // pointers themselves!

Dynamic Memory Allocation int *iPtr; // declaring a pointer variable iPtr = new int(100); // dynamic allocation of a single intege   double *dPtr; // declaring a pointer variable dPtr = new double[20]; // dynamic allocation of an array delete iPtr; // dynamic de-allocation delete[] dPtr; // dynamic de-allocation of an array

Pointers To Pointers Graphically, the construct of a pointer to pointer can be depicted as shown below: pointer_one is the first pointer, pointing to the second pointer, pointer_two and finallypointer_two is pointing to a normal variable num that hold integer 10. char ch = ‘A’; char **secondPtr, *firstPtr; // declare first and second pointers   firstPtr = &ch; // store address of ch in firstPtr secondPtr = &firstPtr; // store the address of firstPtr in secondPtr **secondPtr = ‘B’; // store ‘B’ in ch

Exercise 1 Assuming that the statement int x, *ptr=&x; places the variable x into memory at address 8688 and the ptr variable at 8684. Write down the output you expect for the following code: x=130; cout << x << endl; cout << ptr << endl;   x=131; cout << x << endl; cout << &ptr << endl;   x=132; cout << &x << endl; cout << ptr << endl;   x=133; cout << &x << endl; cout << *ptr << endl;   if(x != *ptr) cout << “Hello1” << endl; else cout << “Hello2” << endl;

Exercise 2 Identify the errors: #include <iostream> void main() { int x, y=131, *ptr; x=132; y=*ptr; cout << x << endl; cout << y << endl; return 0; } #include <iostream>   void main() { int x,y=131, *ptr; x=132; ptr=&x; &y=ptr; cout << x << endl; cout << y << endl; return 0; } #include <iostream> void main() { int x=131,y, *ptr=y; y=132; *ptr=x; cout << x << endl; cout << y << endl; return 0; }

Exercise 2 Identify the errors: #include <iostream> void main() { int x[2]={1,2}, *ptr; ptr=x; ptr=&x[0]; ptr=x[1]; cout << x << endl; cout << y << endl; return 0; } #include <iostream> void main() { int x=1,y=0, *xPtr=&x, *yPtr=&y; if(xPtr != NULL) cout << (*xPtr / *yPtr) << endl; return 0; }

Example 3 Write a program that calls a function to swap two characters and print them in the new form using call by pointer.

Example 4 Write a function that receives a pointer to a character string and a character. The function should return the number of times that the character occurred in the string. Note that a string is an array of characters terminated by the special character ‘\0’. Assume that the function has the following prototype: int char_count (char *ptr, char c)

Example 5 Write a program that reads a data file that contains the time (second) and coordinates of the position of a robot that is roaming in unknown space. The first row stores the number of readings. Each subsequent line includes the time, the x coordinate, and the y coordinate for the robot. The program should determine the times when the robot comes inside a circle centered at the origin with a radius R (entered by the user). Use dynamic memory allocation to create run-time arrays for storing the time, x coordinate, and y coordinate.

Example 6 Write a program that dynamically resize an array to a bigger size. The program creates an array of an arbitrary size (specified by the user). Then the user may extend the array to a bigger size (this involves creating a temporary array with larger size, copying the values of the elements, and adding new values into the extension cells). Print the new array to the user to confirm the functionality.

Example 7 G O L F \0 H O C K E Y \0 F O O T B A L L \0 C R I C K E T \0 S H O O T I N G \0 Write a program that uses double pointer to create an array of strings to store the names of sports as shown in the following figure. Print these strings on the output screen on separate lines. Make sure to deallocate the memory before terminating the program. sports
Tags