DSA Lab Session 3
DSA Lab Session 3
Objective
Pre-lab Exercise
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