Programming Fundamentals
Lab 08
Topic POINTERS,
Practical implementation of the following topics
POINTERS
Objectives
Lab Description:
This lab is basically design for the concepts about pointers
POINTERS:
A pointer however, is a variable that stores the memory address as its value. A pointer variable points to
a data type (like int or string) of the same type, and is created with the * operator. The address of the
variable you're working with is assigned to the pointer.
POINTERS Declaration:
Create a pointer variable with the name ptr, that points to a string variable, by using the asterisk
sign * (string* ptr). Note that the type of the pointer has to match the type of the variable you're
working with. Use the & operator to store the memory address of the variable called food, and assign it
to the pointer. Now, ptr holds the value of declared array in memory address.
There are three ways to declare pointer variables, but the first way is preferred:
string* mystring; // Preferred
string *mystring;
string * mystring;
Datatype
datatype represents the type of the variable that the pointer will point to. For example, if you want to
create a pointer to an integer, you would use int*. Let's look at some examples to better understand
pointers in C++:
1
Example 1: Pointer to an Integer
int num = 10;
int* ptr; // Pointer to an integer
ptr = # // Assign the address of 'num' to the pointer
// Accessing the value using the pointer
cout << "Value of num: " << *ptr << endl;
In this example, we declare a variable num of type int. We then declare a pointer ptr of type int* to
store the address of num. By using the address-of operator (&), we assign the address of num to the
pointer. To access the value stored at the address pointed by ptr, we use the dereference operator (*).
Example 2: Pointer to a Character
char letter = 'A';
char* ptr;
ptr = &letter;
cout << "Value of letter: " << *ptr << endl;
In this example, we declare a variable letter of type char and a pointer ptr of type char*. We assign the
address of letter to the pointer using the address-of operator (&). Then, we print the value of letter
using the pointer and the dereference operator (*).
These are basic examples illustrating the concept of pointers. Pointers can be used for various other
purposes in C++, such as dynamic memory allocation using new and delete operators or creating data
structures like linked lists.
4
Lab Tasks
Task 1
Write a C++ program that takes an integer number from user. Make a pointer of integer type that stores
address of the integer in a pointer variable. Now print the address and also the value stored in pointer
variable.
Task 2
Write a C++ program that takes a float number from user. Make a pointer of void type that stores address
of the float in a pointer variable. Now print the address of float variable and also the value stored in
pointer variable.
Task 3
Write a C++ program that takes two positive integers and make an integer type pointer that calculate sum.
5
Task 4
Write a C++ program that takes two float numbers from user. Make a float type pointer that calculate
result after division.
Task 5
Write a C++ program that takes an integer from user. Make a pointer of integer type that checks whether
the integer entered by user is positive, negative or equal to zero.
Task 6
Write a C++ program that takes a positive integer from user. Make a pointer of integer type that prints
the table of an integer entered by user.
6
Task 7
Write a C++ program that takes an integer from user. Make a pointer of integer type that first increment
(+1) then decrement (-1) in an integer number entered by user.
Task 8
Write a C++ program that takes two positive integers from user and make an integer type pointer that
find the difference between them.
Task 9
Write a C++ program that takes three integers from user and make an integer type pointer that find the
maximum number between them.
7
8