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

Notes

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)
43 views10 pages

Notes

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

PARAMETER PASSING

In C, there are different ways in which parameter data can be passed into and out
of methods and functions. Let us assume that a function B() is called from another
function A(). In this case, A is called the “caller function” and B is called the “called
function or callee function”. Also, the arguments which A sends to B are called actual
arguments and the parameters of B are called formal arguments.
Terminology
● Formal Parameter: A variable and its type as it appears in the prototype of the
function or method.
● Actual Parameter: The variable or expression corresponding to a formal
parameter that appears in the function or method call in the calling environment.
● Modes:
○ IN: Passes info from caller to the callee.
○ OUT: Callee writes values to the caller.
○ IN/OUT: The caller tells the callee the value of the variable, which the
callee may update.
Methods of Parameter Passing in C
There are two ways in which we can pass the parameters to the function in C:
1. Pass By Value
This method uses in-mode semantics. Changes made to formal parameters do not
get transmitted back to the caller. Any modifications to the formal parameter variable
inside the called function or method affect only the separate storage location and will not
be reflected in the actual parameter in the calling environment. This method is also called
call by value.
Example of Pass by Value
The below example demonstrates pass by value in function.
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2 ;
void swap(int,int) ; // function declaration
clrscr() ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(num1, num2) ; // calling function
printf("\nAfter swap: num1 = %d\nnum2 = %d", num1, num2);
getch() ;
}
void swap(int a, int b) // called function
{
int temp ;
temp = a ;
a=b;
b = temp ;
}
Output:
Before swap: num1 = 10, num2 = 20
After swap: num1 = 20, num2 = 10

Shortcomings of Pass By Value:


● Inefficiency in storage allocation
● For objects and arrays, the copy semantics are costly

2. Pass by Pointers
This technique uses a pointer. In function we pass the memory address (pointer) of
a variable rather than passing the actual value of the variable. This passing technique
allows the function to access and modify the content at that particular memory location.
Example of Pass by Pointers
The below program demonstrate the pass by pointer in Function
#include<stdio.h>
#include<conio.h>

void main()
{
int num1, num2 ;
void swap(int *,int *) ; // function declaration
clrscr() ;
num1 = 10 ;
num2 = 20 ;
printf("\nBefore swap: num1 = %d, num2 = %d", num1, num2) ;
swap(&num1, &num2) ; // calling function
printf("\nAfter swap: num1 = %d, num2 = %d", num1, num2);
getch() ;
}
void swap(int *a, int *b) // called function
{
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
}
Output:
Before swap: num1 = 10, num2 = 20
After swap: num1 = 20, num2 = 10

Shortcomings of Pass by Pointers


● Pointers can be null so null pointer issues arise if properly not checked.
● If more than one pointers point to the same memory location then changes made
by one pointer affect the other pointers which point to the same memory location.
● memory management should be done effectively using functions like malloc and
free.
SELF REFERENTIAL STRUCTURES
A self referential structure is used to create data structures like linked lists, stacks,
etc. Following is an example of this kind of structure:
struct struct_name
{
datatype element;
struct_name * pointer_name;
};
A self-referential structure is one of the data structures which refer to the pointer
to (points) to another structure of the same type. For example, a linked list is supposed to
be a self-referential data structure. The next node of a node is being pointed, which is of
the same struct type. For example,
struct node
{
void *data;
struct node *next;
};
In the above example, the node is a self-referential structure – because the *next is
of the type struct node.
Single Linked List:
A linked list is formed when many such nodes are linked together to form a chain.
Each node points to the next node present in the order. The first node is always used as a
reference to traverse the list and is called HEAD. The last node points to NULL.

Declaring a Linked list :


In C language, a linked list can be implemented using structure and pointers.
struct LinkedList
{
int data;
struct LinkedList *next;
};
The above definition is used to create every node in the list. The data field stores
the element and the next is a pointer to store the address of the next node and it is a
self-referencing pointer.
Creating a Node:
Let's define a data type of struct LinkedList to make code cleaner.
typedef struct LinkedList *node;
node createNode()
{
node temp;
temp = (node)malloc(sizeof(struct LinkedList));
temp->next = NULL;
return temp;
}
● typedef is used to define a data type in C.
● malloc() is used to dynamically allocate a single block of memory in C, it is
available in the header file stdlib.h.
● sizeof() is used to determine size in bytes of an element in C.

Adding Node to Linked list


Let's see how to add a node to the linked list:
node addNode(node head, int value)
{
node temp,p;
temp = createNode();
temp->data = value;
if(head == NULL)
{
head = temp;
}
else
{
p = head;
while(p->next != NULL)
{
p = p->next;
}
p->next = temp;
}
return head;
}
Here the new node will always be added after the last node. This is known as
inserting a node at the rear end.
Traversing the list:
The linked list can be traversed in a while loop by using the head node as a starting
reference:
node p;
p = head;
while(p != NULL)
{
p = p->next;
}
Program:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
void printList()
{
struct node *p = head;
while(p != NULL)
{
printf(" %d =>",p->data);
p = p->next;
}
}
void insert(int data)
{
struct node *link = (struct node*) malloc(sizeof(struct node));
link->data = data;
link->next = head;
head = link;
}
int main()
{
insert(10);
insert(20);
insert(30);
insert(1);
insert(40);
insert(56);
printList();
return 0;
}
Output:
56 => 40 => 1 => 30 => 20 => 10
TYPES OF FILE PROCESSING: SEQUENTIAL ACCESS, RANDOM ACCESS
In computer programming, the two main types of file handling are:
● Sequential access
● Random access

Sequential access
In this type of files data is kept in sequential order if we want to read the last
record of the file, we need to read all records before that record so it takes more time.

Sequential access to file


To access a particular data within the sequential access file, data has to be read one
data at a time, until the required data is reached.
Reading data from a sequential file can be done using the various C functions like
fscanf(), fgets(), fgetc(), fread().
Writing data to a sequential file can be done using the various Cfunctions like
fprintf(), fputs(), fputc(), fwrite().
EXAMPLE PROGRAM:
Finding average of numbers stored in sequential access file
#include<stdio.h>
#include<conio.h>
float average(FILE *input)
{
float term,sum;
int n;
sum = 0.0;
n = 0;
while(!feof(input))
{
fscanf(input,"%f",&term);
sum = sum + term;
n = n + 1;
}
return sum/n;
}
int main ()
{
FILE *input;
float avg;
input = fopen("[Link]","r");
avg = average(input);
fclose(input);
printf("The average of the numbers is %f.\n",avg);
return 0;
}
Output:
/* Data in [Link]
10 11 12 13 14 15.
*/
The average of the numbers is 12.5
Random access
In this type of files data can be read and modified randomly .If we want to read the
last record we can read it directly. It takes less time when compared to a sequential file.

Random Access To File

There is no need to read each record sequentially, if we want to access a particular


record. C supports these functions for random access file processing.
1. fseek()
2. ftell()
3. rewind()
fseek():
It is used to move the reading control to different positions using the fseek
function.
Syntax:
fseek( file pointer, displacement, pointer position);
file pointer ---- It is the pointer which points to the file.
displacement ---- It is positive or negative. This is the number of bytes which are
skipped backward (if negative) or forward( if positive) from the current position. This is
attached with L because this is a long integer.
Pointer position:
This sets the pointer position in the file. The three values are,
● 0 - Beginning of file
● 1 - Current position
● 2 - End of file
ftell()
It tells the byte location of the current position of the cursor in file pointer.
rewind()
It moves the control to the beginning of the file.
Example program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("file1.c", "r");
if(fp==NULL)
printf("file cannot be opened");
else
{
printf("Enter value of n to read last ‘n’ characters");
scanf("%d",&n);
fseek(fp,-n,2);
while((ch=fgetc(fp))!=EOF)
{
printf("%c\t",ch);}
}
}
fclose(fp);
getch();
}

You might also like