0% found this document useful (0 votes)
10 views6 pages

lab #9

The document outlines a lab session for Computer Programming at Birla Institute of Technology & Science, focusing on pointers in C programming. It covers topics such as modular programming, passing arguments to functions, recursive functions, and pointer arithmetic, along with examples and exercises. Additionally, it includes tasks for students to implement functions that work with arrays and pointers.

Uploaded by

madhavdalvi06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

lab #9

The document outlines a lab session for Computer Programming at Birla Institute of Technology & Science, focusing on pointers in C programming. It covers topics such as modular programming, passing arguments to functions, recursive functions, and pointer arithmetic, along with examples and exercises. Additionally, it includes tasks for students to implement functions that work with arrays and pointers.

Uploaded by

madhavdalvi06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Birla Institute of Technology & Science, Pilani

Second Semester 2024-2025, Computer Programming [CS F111]


Lab #9

Week #9 [March 24, 2025 To March 27, 2025]

Objectives

1. Modular Programming: Functions in C


2. Passing Arguments to Functions
3. Recursive Functions
4. Exercises

9.0 Introduction to Pointers [ 15-20 mins]


Pointers are variables that hold address of another variable of same data type. Pointers are one of the
most distinct and exciting features of C language. It provides power and flexibility to the language.
Although pointer may appear little confusing and complicated in the beginning, but it is a powerful tool
and handy to use once its mastered.

Benefit of using pointers


• Pointers are more efficient in handling Array and Structure.
• Pointer allows references to function and thereby helps in passing of function as arguments to other
function.
• It reduces length and the program execution time.
• It allows C to support dynamic memory management.

Whenever a variable is declared, system will allocate a location to that variable in the memory, to hold
value. This location will have its own address number.
Let us assume that system has allocated memory location 80F (Hexa decimal representation of memory
address) for a variable a.
int a = 10 ;

We can access the value 10 by either using the variable name a or the address 80F. Since the memory
addresses are simply numbers they can be assigned to some other variable. The variable that holds
memory address are called pointer variables. A pointer variable is therefore nothing but a variable that
contains an address, which is a location of another variable. Value of pointer variable will be stored in
another memory location.
Declaring and initializing a pointer variable:

datatype * identifier ;

int *a;

The meaning of the above statement is that a is a pointer variable that holds address in which integer
data is stored.
Note: a itself is not an integer

Example 1: Write a program to declare pointer variables that point to float data, character data.
Initialize them with appropriate variables. Print the addresses stored and size of memory used by these
pointer variables.

Unlike the variable the pointer variables also will have junk/garbage values. They have to be initialized
before using.

For example:
int a=10;
int *ptra;
ptra=&a; //This statement assigns the pointer address where a is
stored .
*ptra=20; // This statement assign 20 to the memory location pointed by ptra
Note: & is the address operator also called as reference operator and * is called as deference operator

9.1 Pointers and functions [15-20 mins]


There are two ways of passing parameters to functions
1.pass by value
2.pass by reference
Example 2: Demonstrate the implementation of call by value and reference for a program that swaps
two input numbers.
pass by value pass by reference

#include<stdio.h> #include<stdio.h>

void swap(int num1, int num2); void swap(int *num1, int


*num2);
int main() {
int main() {
int x, y;
int x, y;
printf("\nEnter two numbers
: "); printf("\nEnter two numbers
: ");
scanf("%d%d",&x,&y);
scanf("%d%d",&x,&y);
printf("\nBefore Swaping x =
%d and y = %d", x, y); printf("\nBefore Swaping x =
%d and y = %d", x, y);
swap(x, y); // Function Call
- Pass By Reference swap(&x, &y); // Function
Call - Pass By Reference
printf("\nAfter Swaping x =
%d and y = %d", x, y); printf("\nAfter Swaping x =
%d and y = %d", x, y);
return 0;
return 0;
}
}
void swap(int num1, int num2) {
void swap(int *num1, int *num2)
int temp; {
temp = num1; int temp;
num1 = num2; temp = *num1;
num2 = temp; *num1 = *num2;
} *num2 = temp;

Enter two numbers : 2 3 Enter two numbers : 2 3


Before Swapping x = 2 and y = 3 Before Swapping x = 2 and y = 3
After Swaping x = 2 and y = 3 After Swaping x = 3 and y = 2
Conclusion from above example
1. When call by value is used a local copy of the variable is made in the function and the scope those
variables are within the function.
2. When call by reference is used the address of the variables in the main are sent and the function has
pointers refereeing to the sent address hence any change to the content pointed by pointers are
permanent.

9.2 Pointer Arithmetic [15-20 mins]


A pointer in c is an address, which is a numeric value. Therefore, you can perform arithmetic operations
on a pointer just as you can on 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++
After the above operation, the ptr will point to the location 1004 because each time ptr is incremented,
it will point to the next integer location which is 4 bytes next to the current location. This operation will
move the pointer to the next memory location without impacting the actual value at the memory
location.

The address computation is as follows when ptr++ is evaluated:


ptr = ptr + 1 * sizeof(int)
= 1000 + 1 * 4 = 1004
If ptr points to a character whose address is 1000, then the above operation will point to the location
1001 because the next character will be available at 1001.

Example 3: Incrementing and decrementing a Pointer


int arr[] = {10, 100, 1000};
int *ptr1,*ptr2;
ptr1 = arr; /*here arr is an array and implicilty is a pointer
hence & is ignored in case of arrays */
ptr1++; /*assuming that the starting address of the a rray is 1000
this step results in 1004 hence ptr now points to 1004*/
ptr1--; /*since the pointer was pointing to 1004 this step results
in 1000 hence ptr now points to 1000*/
ptr2 = ptr1; //ptr2 will now point to 1000 memory location
if(ptr1==ptr2) //Comparing two pointers
printf(“both pointers are pointing to the same location”);

9.3 Pointers and arrays [20-25 mins]

Arrays are closely related to pointers in C programming but the important difference between them is
that, a pointer variable can take different addresses as value whereas, in case of array it is fixed. This is
demonstrated by the following example:
Example 4:
#include <stdio.h>
int main(){
char c[4];
int i;
for(i=0;i<4;++i){
printf("Address of c[%d]=%x\n",i,&c[i]);
}
return 0;
}
Address of c[0]=28ff44
Address of c[1]=28ff45
Address of c[2]=28ff46
Address of c[3]=28ff47
Notice, that there is equal difference (difference of 1 byte) between any two consecutive elements of
array.

Note: You may get different address of an array.

Relation between Arrays and Pointers


Consider and array:

int arr[4];
Relation between arrays and pointers

In arrays of C programming, name of the array always points to the first element of an array. Here,
address of first element of an array is &arr[0]. Also, arr represents the address of the pointer where it is
pointing. Hence, &arr[0] is equivalent to arr.

Also, value inside the address &arr[0] and address arr are equal. Value in address &arr[0] is arr[0] and
value in address arr is *arr. Hence, arr[0] is equivalent to *arr.

Similarly,

&a[1] is equivalent to (a+1) AND, a[1] is equivalent to *(a+1).


&a[2] is equivalent to (a+2) AND, a[2] is equivalent to *(a+2).
&a[3] is equivalent to (a+1) AND, a[3] is equivalent to *(a+3).
.
.
&a[i] is equivalent to (a+i) AND, a[i] is equivalent to *(a+i).
In C, you can declare an array and can use pointer to alter the data of an array.
Example 5: Write a C program to find the sum of six numbers with arrays and pointers
//Program to find the sum of six numbers with arrays and pointers.
#include <stdio.h>
int main(){
int i,class[6],sum=0;
printf("Enter 6 numbers:\n");
for(i=0;i<6;++i){
scanf("%d",(class+i)); // (class+i) is equivalent to &class[i]
sum += *(class+i); // *(class+i) is equivalent to class[i]
}
printf("Sum=%d",sum);
return 0;
}
Output

Enter 6 numbers:
2
3
4
5
3
4
Sum=21

Task 1: Consider two arrays: x-coor[] and y-coor[]. The ith location of the two arrays stores the x
and y coordinates of a point. For example, x-coor[4] and y-coor[4] stores the x and y coordinates of forth
point.
a) Pass these two arrays to a function which prints the quadrant in which each point lies. Emphasis is on
passing array to a function.
b) Pass these two arrays to a function. The function stores the quadrant in which each point lies in
another array (which is also passed to the function). It then prints the quadrant in main function.
Emphasis is on returning array from a function.

Task 2: Modularize the following program into multiple functions. Consider a list of prices of books
represented as one dimensional array. Traverse the list and modify the prices according to the following
conditions:
(i) if price > 250, offer discount of 10%. Update the price with respect to discount.
(ii) if price > 500, offer discount of 25%. Update the price w.r.t discount.
Modularize it into following functions.
(i) readList(): Populate prices of the items.
(ii) printList(): Prints the price list.
(iii) modifyList(): Modify the prices in the list according to the conditions given above.
Identify input arguments and return types for all the functions. Also write main function where the
functions are called.

You might also like