Data Structures Laboratory Manual
Experiment No. 1
Title
Introduction to Pointers. Call by Value and Call by Reference
Course Code:
ECSCI24101
Course Name:
Data Structures
Aim
1. To understand pointer variables and memory addressing in C, as required for
questions on memory and parameter passing.
2. To analyze the difference between call by value and call by reference with respect to
data modification and memory access.
3. To relate pointer usage with real problems involving arrays, functions, and data
structures.
Software / Hardware Requirements
• Programming Language: C
• Compiler: GCC / Turbo C / Code::Blocks
• Operating System: Windows / Linux
Theory
Pointers
A pointer is a variable that stores the memory address of another variable. Pointers are
tested for: - Address manipulation - Dereferencing - Parameter passing - Relation with
arrays and functions
Syntax:
data_type *pointer_name;
Example:
int a = 10;
int *p = &a;
Here, p stores the address of variable a, and *p gives the value stored at that address.
Call by Value
In call by value, a copy of the actual parameter is passed to the function. Any changes
made inside the function do not affect the original variable.
Key Points: - Changes inside function do NOT affect actual parameters - Separate memory
locations are used - Safe but inefficient for large data structures
Characteristics: - Actual arguments remain unchanged - Safe but less efficient for large
data
Call by Reference
In call by reference, the address of the actual parameter is passed using pointers.
Changes made inside the function reflect in the calling function.
Key Points: - Actual parameters are modified - Same memory location is accessed -
Preferred for arrays and structures
Characteristics: - Original values are modified - Efficient for large data - Uses pointers
Program 1: Demonstration of Pointers
Algorithm
1. Declare an integer variable.
2. Declare a pointer variable.
3. Store the address of integer variable in pointer.
4. Display value and address using pointer.
Program Code
#include <stdio.h>
int main()
{
int a = 10;
int *p;
p = &a;
printf("Value of a: %d\n", a);
printf("Address of a: %p\n", &a);
printf("Value using pointer: %d\n", *p);
printf("Address stored in pointer: %p\n", p);
return 0;
}
Output
Value of a: 10
Value using pointer: 10
Program 2: Call by Value
Algorithm
1. Define a function to swap two numbers using call by value.
2. Pass variables to the function.
3. Display values before and after function call.
Program Code
#include <stdio.h>
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
int main()
{
int a = 10, b = 20;
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(a, b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Output
Before swapping: a = 10, b = 20
After swapping: a = 10, b = 20
Program 3: Call by Reference
Algorithm
1. Define a function to swap two numbers using pointers.
2. Pass addresses of variables.
3. Display values before and after function call.
Program Code
#include <stdio.h>
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int a = 10, b = 20;
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Output
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10
Observation
• In call by value, function parameters use different memory locations than actual
arguments.
• In call by reference, pointer parameters refer to the same memory location as
actual arguments.
• Dereferencing (*) plays a crucial role in modifying actual data.
Result
Thus, the concepts of pointers, call by value, and call by reference were studied and
implemented successfully.
Practice Questions
MCQ 1:
Consider the following C code:
void fun(int *p) {
*p = *p + 10;
}
int main() {
int x = 5;
fun(&x);
printf("%d", x);
}
What is the output?
Options:
A) 5
B) 10
C) 15
D) Compilation Error
Correct Answer: C) 15
Explanation: Address of x is passed, so the original value is modified.
MCQ 2:
Which of the following statements is TRUE?
A) Call by value modifies actual parameters
B) Call by reference uses separate memory
C) Pointers are required for call by reference in C
D) Arrays are passed by value
Correct Answer: C) Pointers are required for call by reference in C
Explanation: Call by reference uses addresses, implemented using pointers in C.
MCQ 3:
What is the output of the following code?
int x = 10;
int *p = &x;
printf("%d %d", x, *p);
A) 10 10
B) Address Value
C) Garbage Value
D) Compilation Error
Correct Answer: A) 10 10
Explanation: *p dereferences pointer and accesses the value of x.