SlideShare a Scribd company logo
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

More Related Content

PPTX
week14Pointers_II. pointers pemrograman dasar C++.pptx
PDF
C Recursion, Pointers, Dynamic memory management
PPT
FP 201 - Unit 6
PPT
Pointer
PPT
Pointer
PPTX
C++ Pointer | Introduction to programming
PPT
ch08.ppt
PPTX
POLITEKNIK MALAYSIA
week14Pointers_II. pointers pemrograman dasar C++.pptx
C Recursion, Pointers, Dynamic memory management
FP 201 - Unit 6
Pointer
Pointer
C++ Pointer | Introduction to programming
ch08.ppt
POLITEKNIK MALAYSIA

Similar to Computer Programming for Engineers Spring 2023Lab 8 - Pointers.pptx (20)

PDF
(4) cpp automatic arrays_pointers_c-strings
PPTX
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
PDF
C++ Course - Lesson 2
PPT
Lecture#9 Arrays in c++
PPTX
C++ Overview PPT
PPT
pointers
PPTX
Lecture 5Arrays on c++ for Beginner.pptx
PPTX
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
PPT
Pointers definition syntax structure use
PPT
Chapter09-10 Pointers and operations .PPT
PPT
Chapter09-10.PPT
PPTX
Pointers in c++
PPTX
C Programming : Pointers and Arrays, Pointers and Strings
PDF
array 2 (1)_merged.pdf
PPTX
Lecture 2: arrays and pointers
PPTX
c++ pointers by Amir Hamza Khan (SZABISTIAN)
PPTX
Chp3(pointers ref)
PPTX
Chp4(ref dynamic)
PPT
C96e1 session3 c++
(4) cpp automatic arrays_pointers_c-strings
Lecture-5_Arrays.pptx FOR EDUCATIONAL PURPOSE
C++ Course - Lesson 2
Lecture#9 Arrays in c++
C++ Overview PPT
pointers
Lecture 5Arrays on c++ for Beginner.pptx
cppt-170218053903.pptxhjkjhjjjjjjjjjjjjjjj
Pointers definition syntax structure use
Chapter09-10 Pointers and operations .PPT
Chapter09-10.PPT
Pointers in c++
C Programming : Pointers and Arrays, Pointers and Strings
array 2 (1)_merged.pdf
Lecture 2: arrays and pointers
c++ pointers by Amir Hamza Khan (SZABISTIAN)
Chp3(pointers ref)
Chp4(ref dynamic)
C96e1 session3 c++
Ad

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Business Ethics Teaching Materials for college
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
RMMM.pdf make it easy to upload and study
PPTX
Institutional Correction lecture only . . .
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Classroom Observation Tools for Teachers
PDF
01-Introduction-to-Information-Management.pdf
PDF
Pre independence Education in Inndia.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Week 4 Term 3 Study Techniques revisited.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
2.FourierTransform-ShortQuestionswithAnswers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Microbial disease of the cardiovascular and lymphatic systems
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Business Ethics Teaching Materials for college
Module 4: Burden of Disease Tutorial Slides S2 2025
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Complications of Minimal Access Surgery at WLH
RMMM.pdf make it easy to upload and study
Institutional Correction lecture only . . .
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Classroom Observation Tools for Teachers
01-Introduction-to-Information-Management.pdf
Pre independence Education in Inndia.pdf
Ad

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

  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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?
  • 6. 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!
  • 7. 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
  • 8. 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
  • 9. 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;
  • 10. 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; }
  • 11. 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; }
  • 12. Example 3 Write a program that calls a function to swap two characters and print them in the new form using call by pointer.
  • 13. 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)
  • 14. 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.
  • 15. 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.
  • 16. 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