0% found this document useful (0 votes)
15 views10 pages

Lab 1 Pointers

The document discusses pointers in C++ including declaration, address operator, dereference operator, null pointers, void pointers, pointer arithmetic, passing pointers to functions, and practice tasks. It provides examples and sample programs to demonstrate pointer concepts and operations.

Uploaded by

Zepox
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
15 views10 pages

Lab 1 Pointers

The document discusses pointers in C++ including declaration, address operator, dereference operator, null pointers, void pointers, pointer arithmetic, passing pointers to functions, and practice tasks. It provides examples and sample programs to demonstrate pointer concepts and operations.

Uploaded by

Zepox
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

IET DEPARTMENT

BS CS Program

Data Structure and


Algorithms
Lab Manual
Lab 1: Pointers
Objective
o Memory addresses
o Pointers
o Types of pointers
o Pointers Examples
o Practice Task on pointers

Introduction
Computer Memory

Each variable is assigned a memory slot (the size depends on the data type) and the variable’s
data is stored there

Pointers:

Pointers are a type of variable that allow you to specify the address of a variable. They
provide a convenient means of passing arguments to functions and for referring to more
complex data types such as structures. You need to declare and initialize pointers just as you
would other variables, but there are special operators that you need to use.

Activity Time boxing

Task No. Activity Name Activity time Total Time


Lab Manual Lecture 15 mins
Examples 15 mins
Walkthrough Tasks 15 mins
Practice Tasks 100 mins
Tasks Evaluation/Viva 35 mins 180 minutes

Lab Manual Lecture [Expected time = 15 minutes]


Concept Map
Basic Pointer Revision

 Declaration of Pointer variables


type* pointer_name;
//or
type *pointer_name

Address Operator (&): The "address of " operator (&) gives the memory address of the
variable.

The * Operator Dereference: The star operator (*) dereferences a pointer. The * is a
unary operator which goes to the left of the pointer it dereferences. The pointer must have a
pointee, or it's a runtime error.

Test Program:

void PointerTest() {
// allocate three integers and two pointers
int a = 1; int b = 2; int c = 3; int* p; int* q;
// Here is the state of memory at this point.
// T1 -- Notice that the pointers start out bad...
Pointer to Pointer
Types of Pointers

i) Null Pointer

A pointer should be set to zero when it is not assigned to a valid address. Such a pointer is
called a null pointer. Doing this will allow you to check whether the pointer can be safely
dereferenced, because a valid pointer will never be zero.

ii) Void Pointer

A void pointer is a pointer that has no associated data type with it. A void pointer can hold
address of any type and can be type casted to any type.

int a = 10;
char b = 'x';
void *p = &a; // void pointer holds address of int 'a'
p = &b; // void pointer holds address of char 'b'.

How to increment pointer address and pointer’s value?

When we are accessing the value of a variable through pointer, sometimes we just need to
increment or decrement the value of variable though it or we may need to move the pointer to
next int position (just like we did above while working with arrays). The ++ operator is used
for this purpose. One of the examples of ++ operator we have seen above where we traversed
the array using pointer by incrementing the pointer value using ++ operator.

Pointer Arithmetic

pointer is an address which is a numeric value; therefore, you can perform arithmetic
operations on a pointer just as you can a numeric value. There are four arithmetic operators
that can be used on pointers: ++, --, +, and -
To understand pointer arithmetic, let us consider that ptr is an integer pointer which points to
the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation
on the pointer −

ptr++

the ptr will point to the location 1004 because each time ptr is incremented, it will point to the
next integer. This operation will move the pointer to next memory location without impacting
actual value at the memory location. If ptr points to a character whose address is 1000, then
above operation will point to the location 1001 because next character will be available at
1001.

Sample Program
Incrementing Pointer

Decrementing Pointer
Pointers as Strings

A string is a sequence of characters. A string type variable is declared in the same manner as
an array type variable is declared. This is because the string is an array of character type
variables.

Passing Pointers as Arguments to Functions:

The pointer variables can also be passed to functions as arguments. When a pointer variable
is passed to a function the address of the variable is passed to the function. Thus a variable is
passed to a function not by its value but by its reference.
Sample Program

Write a program to pass two parameters to the function to add a constant value of 100
to the passed values using pointers.
In the above program the function “temp” has two parameters which are pointers and are of
int type. When the function “temp” is called the addresses of the variables “a” and “b” are
passed to the function. In the function a value 100 is added to both variables “a” and “b”
through their pointers. That is the previous values of variables “a” and “b” are increased by
100. When the control returns to the program the value of variable a is 110 and that of
variable b is 120.

Practice Tasks

Time: 100 minutes


Task 1:

Write a program to convert Fahrenheit temperature to Celsius degree by passing pointers as


arguments to the function.

Task 2:

Write a program to find length of string by using pointers.

Task 3:

Write a program to input data into an array and find out the maximum value from array
through pointers.
Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks.
Each task is assigned the marks percentage which will be evaluated by the instructor in the
lab whether the student has finished the complete/partial task(s).

Evaluation of Lab

Sr. No. Task No. Mark Total


1 Task 1 10
2 Task 2 10
3 Task 3 10
4 Task 4 10
5 Task 5 10

Outcomes:

The outcomes of this lab were:


a) Students learn Pointers in C++
b) Learn and implement Pointers operations on array, strings etc.
c) Understand pointers types in C++

-: Assignment Task :-
Write a program that dynamically allocates an array large enough to hold
a user defined number of test scores. Once all the scores are entered, the
array should be passed to a function that sorts them in descending order
and display the sorted list. Another function should be called that
calculates the average score and display in the same function. Use
appropriate headings for function and pointer notation rather than array
notation whenever possible.

Input Validation: Do not accept negative numbers for test scores

You might also like