Module 8

Pointers & Files

Home > Courses > CP Lab > Pointers & Files

Programs with line-by-line explanations

Prepared by: Dr. Tojo Mathew

Warning! You are warned against blindly copying the code statements. Read the explanations given for the code statements and understand the implementation. Then, try to do it yourself. Blindly re-typing the code statements is a waste of time & effort.

Table of contents

Note: Descriptions in brown font enclosed within  /*  and  */  or starting with // are comments for human readers  to understand the program better.  No need to type in comments in the actual program. Compilers ignore comments while generating executable code of the program. 

8.1 Swapping two numbers

Mrs. Anitha , our favourite Maths teacher wanted to teach her students to swap two elements.

Write a program to accept 2 integers and to swap them using functions and pointers.

Function Specification:

void swap(int *a, int *b)

This function swaps 2 integers.

Input Format:

The input consists of 2 integers.

Output Format:

Refer to the sample input and output for formatting details.

[All text in bold corresponds to input and the rest corresponds to output.]

Sample Input and Output:

Enter the value of a

5

Enter the value of b

3

Before swapping

a = 5 b = 3

After swapping

a = 3 b = 5

Function Definitions:

Solution: 

General description of program: Exchange the values stored by the user using call-by reference and pointer concepts.

Solution idea: Define a new function (e.g. swap) with two integer pointer parameters. From the main function, call this function with the address of variables stored with the numbers to be swapped as arguments.  In the swap function, exchange the values by dereferencing the pointers. 

8.2 Sum of array elements

Write a program to find the sum of the elements in an array.


Function Specification:

int sum(int *arr,int n)

This function adds all the elements in an array. And returns the added Sum.


Input Format:

Input consists of n+1 integers. The first integer corresponds to ‘n’,the size of the array. The next ‘n’ integers correspond to the elements in the array. Assume that the maximum value of n is 15.

Output Format:

Refer sample output for better understanding.

Sample Input 1:

5

2

3

6

8

1

Sample Output 1:

The sum of the elements in the array is 20


Solution:

General description of program: There is a correspondence between a 1-D array and a pointer. An array name is treated implicitly as a pointer by the C compiler. Hence, if we pass an array as the argument to a function, the formal parameter in the function definition can be a pointer type. This property is used in this program to pass an array to a function, and the formal parameter is of pointer type. The sum of elements in the array is computed in the user defined function and returned to main(). 

8.3 Copy the files

Write a program to copy the content from one file to another file.


Rules:

The input file should be named as "input.txt".

The output file should be named as "output.txt".


Sample Input file (input.txt):

Hello World!

Welcome to C


Sample Output file (output.txt)

Hello World!

Welcome to C


Solution:

General description of program: Creating a copy of text file using file management functions in C.