Unit 5 PPS
Unit 5 PPS
variable
Pointer declaration:
syntax:
Data type *variable name;
Example:
int *x; // x is pointer to integer (means x is a pointer which will store address of a
integer variable)
Pointer notation:
int i=3;
We see that the computer has selected memory location 65524 as the place to store the
value 3.
EXAMPLE 1:
#include<stdio.h>
void main ( )
int i = 3 ;
Address of i = 65524
Value of i = 3
Value of i = 3
EXAMPLE 2: consider i j
#include<stdio.h> 3 65524
void main( ) 65524 65522
{int i = 3 ;
int *j ;
j = &i ;
Address of i = 65524
Address of i = 65524
Address of j = 65522
Value of j = 65524
Value of i = 3
Value of i = 3
Value of i = 3
USE OF POINTER IN 'C'
EXAMPLE 3:
#include<stdio.h>
#include<conio.h>
void main()
ptr1 = &num1;
ptr2 = &num2;
getch();
Applications of Pointer:
POINTER TO POINTER:
When a pointer variable store the address of another pointer variable then it is called
pointer to pointer.
j=&i;
k=&j
POINTER ARITHMETIC:
1. malloc( )
2. calloc( )
3. realloc( )
4. free( )
static memory allocation dynamic memory allocation
malloc( )
Syntax:
Pointer_variable= (cast-type*) malloc(byte-size)
For Example: int *x;
x = (int*) malloc(100 * sizeof(int));
Since the size of int is 2 bytes, this statement will allocate 200 bytes of
memory. And, the pointer ptr holds the address of the first byte in the
allocated memory.
calloc( )
Syntax:
Pointer_variable = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is the size of each element.
For Example: float *x;
x= (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each with the size
of the float.
realloc( ):
“realloc” or “re-allocation” method in C is used to dynamically change the memory
allocation of a previously allocated memory. In other words, if the memory previously
allocated with the help of malloc( ) or calloc( ) is insufficient, realloc( ) can be used
to dynamically re-allocate memory.
Syntax:
Pointer_variable = realloc(pointer_variable, newSize);
free( )
“free” method in C is used to dynamically de-allocate the memory. The memory allocated
using functions malloc() and calloc() is not de-allocated on their own. Hence the free()
method is used
Syntax:
free(pointer_variable);
EXAMPLE: WAP TO FIND THE SUM OF N NUMBERS USING DYNAMIC MEMORY
ALLOCATION
#include<stdio.h>
#include<stdlib.h>
void main()
{ int n,i,*ptr,sum=0;
printf ("Enter number of elements: ");
scanf("%d",&n);
ptr= (int*) malloc(n*sizeof(int));
if (ptr==NULL)
{
printf ("unable to allocate memory");
exit (0);
}
printf("Enter elements of array: ");
for(i=0;i<=n-1;++i)
{ scanf("%d",ptr+i);
sum=sum+*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
}
C PREPROCESSOR:
The program that sorts out all the directives before compilation is called preprocessor
ROLE OF PREPROCESSOR
The statement that begin with # symbol are called preprocessor directives. These are
divided into three categories in C
macro is an identifier name which replaces its value whereever it appears in a program. it
begins with # define
TYPES OF MACRO:
1. SIMPLE MACRO: it is also known as symbolic constant
syntax: # define macro_ name replacement_list
ex: WAP TO FIND THE AREA OF CIRCLE USING MACRO
#include<stdio.h>
#include<conio.h>
#define PI 3.1416//macro
void main()
{
float area, radius;
clrscr();
printf("Enter the radius of circle ");
scanf("%f",&radius);
area=PI*radius*radius;
printf("\nThe Area of the Circle is = %f",area);
getch();
}
2.FUNCTION LIKE MACRO:
A macro with argument is known as function like macro.
syntax: # define macro_name(argument) replacement_list
EX: WAP TO FIND THE SQUARE OF A NO.
#include <stdio.h>
#include <conio.h>
#define SQR(x) (x * x)
void main()
{
int x,s;
printf("Enter any number to find square");
scanf("%d", &x);
s= SQR(x);
printf("SQUARE = %d",s);
getch();
}
3.NESTED MACRO:
when a macro can be used in another macro then it is called nested macro.
#include <stdio.h>
#include <conio.h>
#define SQR(x) (x * x)
#define CUBE(x) (SQR(x)*x)
void main()
{
int x,s;
printf("Enter any number to find cube");
scanf("%d", &x);
s= CUBE(x);
printf("CUBE = %d",s);
getch();
}
2)CONDITIONAL COMPILATION:
CONDITIONAL
DIRECTIVES Used to give default statements with
# else #if,#ifdef and #ifndef
#include<stdio.h>
#define marks 60
void main()
#if marks>80
printf(“distinction”);
#elif marks>=60
printf(“first division”);
#else
printf(“unsuccessful”);
#endif
The directive that are used to link header file in a program are called file inclusion
directives. The directive #include is used to include header file in a program.
Linked-List
Advantages of Linked-List
Here are a few advantages of a linked list that is listed below.
Dynamic Data structure: The size of memory can be allocated or de-allocated at run
time based on the operation insertion or deletion.
Ease of Insertion/Deletion: The insertion and deletion of elements are simpler than
arrays since no elements need to be shifted after insertion and deletion, just the address
needed to be updated.
Efficient Memory Utilization: As we know Linked List is a dynamic data structure the
size increases or decreases as per the requirement so this avoids the wastage of
memory.
Implementation: Various advanced data structures can be implemented using a linked
list like a stack, queue, graph, hash maps, etc.
Doubly Linked List – In this type of linked list, one can move or traverse the linked list
in both directions (Forward and Backward)
Circular Linked List – In this type of linked list, the last node of the linked list contains
the link of the first/head node of the linked list in its next pointer.
struct node
{
int data;
struct node* next;
};
Doubly Circular Linked List – A Doubly Circular linked list or a circular two-way linked
list is a more complex type of linked list that contains a pointer to the next as well as
the previous node in the sequence. The difference between the doubly linked and circular
doubly list is the same as that between a singly linked list and a circular linked list. The
circular doubly linked list does not contain null in the previous field of the first node.
/* Node of a doubly circular linked list */
struct node
int data;
};
The arguments passed from command line are called command line arguments. These
arguments are handled by main () function.
To support command line argument, we need to change the structure of main () function as
given below.
Here, argc counts the number of arguments. It counts the file name as the first argument.
The argv[] contains the total number of arguments. The first argument is the file name
always.
#include<stdio.h>
#include<conio.h>
int i;
for(i=0;i< argc;i++)
getch ();
}
OUTPUT:
C:/TC/BIN>TCC cmdprogram.c
C:/TC/BIN>cmdprogram 10 20
Number of Arguments: 3
0 arguments c:/tc/bin/cmdprogram.exe
1 arguments: 10
2 arguments: 20
WHAT IS A FILE?
A named collection of data stored in secondary storage (typically). The output of a C program
is generally deleted when the program is closed. Sometimes, we need to store that output
for purposes like data analysis, result presentation, comparison of output for different
conditions, etc. The use of file handling is exactly what the situation calls for.
Files are stored as a sequence of bytes logically contiguous. The last byte of a file
contains the end of file character (EOF), while reading a text file, the EOF character can be
checked to know the end.
There are two types of files in c
1. TEXT FILE: contain ASCII codes only. Text files are the normal .txt files that we can easily
create using Notepad or any simple text editors. When we open those files, we'll see all the
contents within the file as plain text. You can easily edit or delete the contents. They take
minimum effort to maintain, are easily readable, and provide least security and takes bigger
storage space.
2. BINARY FILE: can contain non-ASCII characters, image, audio, video, executable etc.
Binary files are mostly the .bin files in our computer. Instead of storing data in plain text,
they store it in the binary form (0's and 1's).They can hold higher amount of data, are not
readable easily and provides a better security than text files.
To check the end of file here the file size value needs to be checked
FILE HANDLING IN C
In C we use FILE * to represent a pointer to a file. Following is the general format for declaring
and opening file.
FILE *fptr note: *fptr is file pointer
fptr=fopen(“file name”,mode);
fputc() fputc(ch,fptr)
where fptr is file type pointer to write a character into a
and ch is character variable file
r Reading – NULL
Loader: Loader is a program that loads the executable file into the system memory for
execution. Loader loads executable file with absolute format by simply copying it into system
memory.