0% found this document useful (0 votes)
23 views41 pages

C08 Psap PPT2 21221

The document discusses pointers, memory allocation, and file handling in C. It covers: 1) What pointers are and how to declare and use them, including pointers to arrays, functions, and structures. 2) Call by value vs call by reference. 3) Dynamic memory allocation functions like malloc(), calloc(), realloc(), and free(). 4) File handling functions like fopen(), fprintf(), fscanf(), fclose(), and how to open, read, write, and close files in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views41 pages

C08 Psap PPT2 21221

The document discusses pointers, memory allocation, and file handling in C. It covers: 1) What pointers are and how to declare and use them, including pointers to arrays, functions, and structures. 2) Call by value vs call by reference. 3) Dynamic memory allocation functions like malloc(), calloc(), realloc(), and free(). 4) File handling functions like fopen(), fprintf(), fscanf(), fclose(), and how to open, read, write, and close files in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Pointers, Memory allocation and

File Handling

Presented by Group C8
• 37 - Sanket Bhosle 38 - Anurag Bhujbal
• 39 - Prathamesh Bhujbal 40 - Kshitij Bhure
• 41 - Dhananjay Bile 42 - Ayush Billade
• Guide’s name – Amol Bhosle
Outline

 Pointers and its application


 Call by value
 Call by Reference
 Passing arrays to a function in C
 Dynamic memory allocation
 File Handling
What is a pointer ?

 Pointers (pointer variables) are special variables that are used to


store addresses rather than values. 
 This variable can be of type int, char, array, function, or any
other pointer. 
 The size of the pointer depends on the architecture.
However, in 32-bit architecture the size of a pointer is 2 byte.
How to Use Pointers?

 There are a few important operations, which we will do with the help of pointers very
frequently. 
 (a) We define a pointer variable,
 (b) assign the address of a variable to a pointer  
 (c) access the value at the address available in the pointer variable. This is done by
using unary operator * that returns the value of the variable located at the address
specified by its operand.
How do we Declare a Pointer

 The pointer in c language can be declared using * (asterisk symbol). It is also known as
indirection pointer used to dereference a pointer.
1. int *a; //pointer to int  
2. char *c; //pointer to char
3. float *s; //pointer to float  
4. double *db; //pointer to double
Understanding pointer with help of Ex

 Program to implement the use of pointers :

Output:
Understanding pointers

• This figure explains more about how the pointers works


Usage of pointers

 Pointer to array:

 Pointer to a function:

 Pointer to structure:
Advantage of pointer

 Pointer reduces the code and improves the performance, it is used to retrieving strings, trees,


etc. and used with arrays, structures, and functions.
 We can return multiple values from a function using the pointer.
 It makes you able to access any memory location in the computer's memory.
 Dynamic memory allocation : we can dynamically allocate memory using malloc() and calloc() functions
where the pointer is used.
 Pointers are widely used in arrays, functions, and structures. It reduces the code and improves the
performance.
Call By Value

 Here values of actual parameters will be copied to formal parameters and these two different parameters stores
values in different locations.

int x = 10, y=20; int fun(int x, int y)


fun(x,y); {
Printf(“x=%d, y = %d”,x,y); x= 20;
y= 10; }
10 20

x y 20 10
x y
Output: x = 10 , y = 20
CALL BY REFERENCE

 Here both the actual and formal parameter refers to same memory location .Therefore , any changes made to
the formal formal parameters will get reflected to actual parameters.

 Here instead of passing values, we pass address


Example
int fun(int *ptr1, int *ptr2)
int x = 10, y=20; {
*ptr1 = 20 ;
fun(&x, &y); *ptr2 = 10;
}

x y x y ptr1 ptr2

20 10 10 20 1000 2000

1000 2000 1000 2000


Program
o #include <stdio.h>
o void swap(int *x, int *y) {
o int main () { o int temp;

o temp = *x;
o *x = *y;
o int a = 100; o *y = temp;
o o  Output
int b = 200;
o return;
o o }
o printf("Before swap, value of a : %d\n", a );
o printf("Before swap, value of b : %d\n", b );
o
o /* calling a function to swap the values */
o swap(&a, &b);
o
o printf("After swap, value of a : %d\n", a );
o printf("After swap, value of b : %d\n", b );
o
o return 0;
o }
Passing arrays to a function in C :

1) Passing Individual Array Elements:

Passing array elements to a function is similar to passing variables to function.


Example :

Output
Passing Arrays to Functions
Example :
Output :
Pass Multidimensional Arrays to a Function
Example:

Output :
Basics of Dynamic Memory
Allocation:
Static memory allocation:

 Memory allocated during compile time is called as static memory .


 The memory allocated is fixed during run time.

 Example:
Disadvantages of static memory allocation:

 Size is fixed and the user cannot increase or decrease the size of the array at run time.
 If values stored is less than the size specified then there will be wastage of memory.
 If the values stored are more than the size specified then the program may crash or misbehave.
Dynamic memory allocation:

 The process of allocating memory at the time of execution is called as dynamic memory allocation.

 Built in functions :
1. malloc()
2. calloc()
3. realloc()
4. free() .
What is malloc() ?

 The malloc() or “memory allocation” method in C is used to dynamically allocate a single large block of


memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.
It doesn’t Initialize memory at execution time so that it has initialized each block with the default garbage
value initially. 
 If space is insufficient, allocation fails and returns a NULL pointer.

 Syntax: ptr = (cast-type*)malloc(byte-size)

 Example: ptr = (int*)malloc(100*sizeof(int));


Program:
 Output :
What is calloc() ?

 The calloc() or “contiguous allocation” method in C is used to dynamically allocate the specified number of


blocks of memory of the specified type. it is very much similar to malloc() but has two different points and
these are:
 It initializes each block with a default value ‘0’.
 It has two parameters or arguments as compare to malloc().
 If space is insufficient, allocation fails and returns a NULL pointer.

 Syntax : ptr = (cast-type*)calloc(n, element-size);

 Example: ptr = (float*)calloc(25,sizeof(float));


Program:

 Output:
What is realloc() ?

 The 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. re-allocation of memory
maintains the already present value and new blocks will be initialized with the default garbage value.
 If space is insufficient, allocation fails and returns a NULL pointer.

 Syntax: ptr = realloc(ptr, newSize);

 Example: int *ptr = (int *)malloc(sizeof(int));

 ptr = (int *)realloc(ptr, 2*sizeof(int));


Program:

 Output:
What is free() ?

The memory occupied by malloc() or calloc() functions must be


released by calling free() function. Otherwise, it will consume
memory until program exit.

Syntax : free(ptr)  
Example

int main ()
{
int *ptr = (int*)malloc(4*size of (int));
…..
free(ptr);
}
Program:

o #include<stdio.h>  
o #include<stdlib.h>  
o int main(){  
o  int n,i,*ptr,sum=0;    
 Output
o     printf("Enter number of elements: ");    
o     scanf("%d",&n);    
o     ptr=(int*)malloc(n,sizeof(int));  //memory allocated using malloc    
o    
o     printf("Enter elements of array: ");    
o     for(i=0;i<n;++i)    
o     {    
o         scanf("%d",ptr+i);    
o         sum+=*(ptr+i);    
o     }    
o     printf("Sum=%d",sum);    
o     free(ptr);
o ptr = NULL;  
o return 0;  
o }     
File Handling

 Creation of the new file

File I/O
 Opening an existing file 
 Reading data from the file
 Writing data to the file
 Moving data to a specific location on the file
 Deleting the file
Types of files and syntax

1. Text Files - Normal text files whose contents can be read as plain text
2. Binary Files – Bin files that store data in binary form i.e. 0’s and 1’s which cannot be read as
plain text

Declaration of a file:
File *fptr;
Functions for File Handling

No. Function Description


1 fopen() opens new or existing file
2 fprintf() write data into the file
3 fscanf() reads data from the file
4 fputc() writes a character into the file

5 fgetc() reads a character from file

6 fclose() closes the file


7 fseek() sets the file pointer to given
position
Functions for File Handling

No. Function Description


8 fputw() writes an integer to file
9 fgetw() reads an integer from file
10 ftell() returns current position
11 rewind() sets the file pointer to the beginning
of the file
Opening a File : fopen()

 Used to open a file

 Syntax :
File *fptr
fptr = fopen(“file_name”, “mode”)

 Above function accepts 2 parameters – file name and mode in which file is to be opened (Both the
parameters are strings)
Modes

Mode Description
r opens a text file in read mode
w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and append mode
rb opens a binary file in read mode
wb opens a binary file in write mode
ab opens a binary file in append mode
rb+ opens a binary file in read and write mode
wb+ opens a binary file in read and write mode
ab+ opens a binary file in read and append mode
Important Points

 Always close a file using int fclose( FILE *fp ); syntax


 EOF – End of File
 If file doesn’t exist then fopen() returns NULL
 For writing and appending, if file doesn’t exist then it will be created.
Writing data on a file

Input: Output:

Text file:
Example

Input: Output:

Text file:
Applications of File Handling

1. Bank Management System


2. Contact Management System
3. Employee Record Management System
4. Student Record Management System
Thank you!

You might also like