0% found this document useful (0 votes)
7 views12 pages

C PROGRAMMING File Pointers

Uploaded by

kauxhiii
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)
7 views12 pages

C PROGRAMMING File Pointers

Uploaded by

kauxhiii
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/ 12

GROUP PRESENTATION

Subject Name: PROGRAMMING IN C Subject Code: 231CSC201T

FILE POINTERS AND POINTER


PRESENTED BY, VARIABLES
(066) KARTHI RAMAN M (069) KAUSHIK
(067) KARTHIK SRINIVAS S (070) KEERTHANA SHREE VENUGOPAL
(068) KARTHIKEYAN

EASWARI ENGINEERING COLLEGE


(Autonomous)
Ramapuram, Chennai – 600 089
Tamil Nadu
24rth MAY 2024
INTRODUCTION TO POINTERS IN C
DEFINITION:
A pointer is defined as a derived data type that can store the address of other C variables
or a memory location. We can access and manipulate the data stored in that memory
location using pointers.

Syntax:
The syntax of pointers is similar to the variable declaration in C, but we use the ( * )
dereferencing operator in the pointer declaration.

datatype * ptr;

where,
ptr is the name of the pointer.
datatype is the type of data it is pointing to.
POINTER ARRITHEMATICS IN C

• Pointer Arithmetic is the set of valid arithmetic operations that can be


performed on pointers. The pointer variables store the memory address of
another variable. It doesn’t store any value.
• These operations are:
1.Increment/Decrement of a Pointer
2.Addition of integer to a pointer
3.Subtraction of integer to a pointer
4.Subtracting two pointers of the same type
5.Comparison of pointers
INCREMENT/ DECREMENT OF A POINTER

• Increment: It is a condition that also comes under addition. When a pointer is


incremented, it actually increments by the number equal to the size of the data
type for which it is a pointer.

• For Example:
int arr[3] = {1, 2, 3};
int *ptr = arr; // ptr points to the first element of the array
ptr++; // ptr now points to the second element of the array

arrays.
INCREMENT/ DECREMENT OF A POINTER

• Decrement: It is a condition that also comes under subtraction. When a pointer is


decremented, it actually decrements by the number equal to the size of the data
type for which it is a pointer.

• For Example:

int arr[3] = {1, 2, 3};


int *ptr = &arr[1]; // ptr points to the second element of the array
ptr--; // ptr now points to the second element of the array
DYNAMIC MEMORY ALLOCATION

• Essential for creating flexible and efficient programs, particularly in scenarios


where the required memory size is not known beforehand.
• Key Concept: Allows programs to use memory more effectively, as memory can
be allocated and deallocated as needed during program execution.
• Real-World Applications:
• Data Structures: Dynamic arrays, linked lists, trees, and graphs.
• Large Data Handling: Managing large datasets that grow or shrink over time.
• Resource Management: Memory management in operating systems and
applications.
FUNCTIONS FOR DYNAMIC MEMORY ALLOCATION

• malloc :
Allocates a specified number of bytes and returns a pointer to the first byte.
• calloc :
Allocates memory for an array of elements, initializes them to zero.
• realloc :
Resizes the memory block pointed to by the pointer.
• free (Deallocation):
Frees the allocated memory.
FILE POINTERS

• A file pointer points to a variable of datatype FILE which is a data


type in C standard I/O library that represents a file stream.

• DECLARATION:
FILE *filePointer;

• It is typically initialized by opening a file using the fopen function


FILE *fp;
fp = fopen("example.txt", "r");
FILE OPERATIONS USING POINTERS
• Reading from Files:
Functions like fgetc, fgets and fscanf are used to read data from a file.
Ex:
char buffer[100];
fgets(buffer, 100, fp);

• Writing to Files:
Functions like fputc, fputs and fprintf are used to write data to a file.
Ex:
fprintf(fp, "Hello, World!");
WORKING WITH FILE POINTERS

File pointers can be used to perform various functions.


• fopen() : Opens a file with the specified access mode associated with pointer
Syntax:
FILE *fp = fopen(“File_Name”,”access_mode”);
• fclose() : Closes a file with the associated file pointer .
Syntax:
fclose(fp);
• fprintf() : It is used to write formatted output to the file specified by the pointer
Syntax :
fprintf(FILE *fp, const char *format , arugment);
• fscanf() : It is used to read formatted inputs from a file .
Syntax:
fscanf( FILE *stream, const char *format , argument);

• fgetc() : It is used to read a single character form the file.


Syntax:
fgetc(FILE *stream);

• fgets() : It is used to read a single line from the file.


Syntax :
fgets(chat *str , int n , FILE *stream);
ADVANCED FILE OPERTAIONS
Some of the advanced file operations are fseek() , ftell() , rewind() as they allow the user to
have precise control over the postion of the pointer .

• fseek() : It is used to explicitly move the pointer to a specified location ./


Syntax:
fseek(FILE *stream , offset , whence);

• ftell() : It is used to return the current position of the filer poiner .


Syntax :
ftell(FILE *stream);

• rewind() : It is used to move the file pointer to the beginning of the file .
Syntax :
rewind(FILE *stream);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy