0% found this document useful (0 votes)
23 views

DSA Lab Session 3

The document discusses pointers in C++. It covers declaring and using single pointers, pointers in arrays and structures, passing pointers to functions, pointer arithmetic, and accessing structure members through pointers. It includes example code and questions to help learn about various pointer concepts.

Uploaded by

beshahashenafe20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

DSA Lab Session 3

The document discusses pointers in C++. It covers declaring and using single pointers, pointers in arrays and structures, passing pointers to functions, pointer arithmetic, and accessing structure members through pointers. It includes example code and questions to help learn about various pointer concepts.

Uploaded by

beshahashenafe20
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab session 3: POINTERS

Objective

The objective of lab session 3 is


 To declare and use a single pointer
 To use a pointer in array
 To use a pointer in structure
 To use a pointer in a function

Pre-lab Exercise

1. Which of the following is true about pointers


a. A pointer is a variable which stores the address of another variable.
b. The * operator gives us the address of a variable.
c. The actual data type of the value of all pointers, whether integer, float,
character, or otherwise, is the same, a long hexadecimal number that
represents a memory address.
d. The & operator gives us the value of a variable at a specified address.
2. A below diagram illustrate the relationships between 3 variables x, i and y. Each
variable has a name, a memory address and value.

a. What is the value of &x, x and *x respectively


b. What is the value of &i and i
c. What is the value of &y, y and *y respectively

1|Page
3. Explain the error for the below fragment of code
char c= ‘A’;
double *p = &c;
4. Assume the below definitions and initializations:
char c = 'T', d = 'S';
char *p1 = &c;
char *p2 = &d;
char *p3;
Assume further that the address of c is 6940, the address of d is 9772, and the
address of e is 2224. What will be printed when the following statements are
executed sequentially?
p3 = &d;
cout << "*p3 = " << *p3 << endl; // (1)
p3 = p1;
cout << "*p3 = " << *p3 << ", p3 = " << p3 << endl; // (2)
*p1 = *p2;
cout << "*p1 = " << *p1 << ", p1 = " << p1 << endl; //(3)
5. Write a statement to access structure members using the pointer ptr
struct Distance
{
int feet;
float inch;
};

Distance *ptr;
6. Write block of code that take an input from the user and display the same value
using pointer.
7. Write a program that asks the user to enter integers as inputs to be stored in the
variables 'a' and 'b' respectively. There are also two integer pointers named ptrA
and ptrB. Assign the values of 'a' and 'b' to ptrA and ptrB respectively, and
display them.

2|Page
In-lab Exercise

8. Write a C++ program to accept five integer values from keyword and print the
elements of the array in reverse order using a pointer.
9. Write a function countEven(int*, int) which receives an integer array and its size,
and returns the number of even numbers in the array.
10. Write two functions void cubeByPtr1 ( int * numPtr ) and void cubeByPtr2 ( int &
numPtr ) which returns a cube of a number.

Post-lab Exercise

11. What are the basic differences between normal variable and pointer variable?
12. Discus with example about pointer arithmetic in C++.

3|Page

You might also like