Chapter 1 OOP
Chapter 1 OOP
Chapter:1
Pointers in C, Dynamic Memory Allocation and File
management
int main(){
int number=50;
int *p;//pointer to int
int **p2;//pointer to pointer
p=&number;//stores the address of number variable
p2=&p;
printf("Address of number variable is %x \n",&number);
printf("Address of p variable is %x \n",p);
printf("Value of *p variable is %d \n",*p);
printf("Address of p2 variable is %x \n",p2);
printf("Value of **p2 variable is %d \n",*p);
return 0;
}
Program to to swap two number using pointer
#include<stdio.h>
int main( )
{
int a, b, temp ;
int *p1, *p2 ;
printf(" Enter the first number : ") ;
scanf("%d",&a) ;
printf("\n Enter the second number : ") ;
scanf("%d",&b) ;
p1=&a;
p2=&b;
printf("\n Two Number before swapping :%d, %d ",*p1, *p2) ;
temp = *p1 ;
*p1 = *p2 ;
*p2 = temp ;
printf("\n Two Number after swapping :%d, %d ",*p1, *p2) ;
Pointer to Array
#include<stdio.h>
void main()
{
int a[3] = {1, 2, 3};
int *p = a;
for (int i = 0; i < 3; i++)
{
printf("%d", *p);
p++;
}
return 0;
}
Pointer Arithmetic
• Following arithmetic operations are possible on the pointer:
• Increment
• Decrement
• Addition
• Subtraction
Incrementing Pointer
• If we increment a pointer by 1, the pointer will start pointing
to the immediate next location.
void *ptr;
The void pointer in C cannot be dereferenced directly.
#include <stdio.h>
int main()
{
int a=90;
void *ptr;
ptr=&a;
printf("Value which is pointed by ptr pointer : %d",*ptr);
return 0;
}
#include <stdio.h>
int main()
{
int a=90;
void *ptr;
ptr=&a;
printf("Value which is pointed by ptr pointer : %d",*(int*)ptr);
return 0;
}
we typecast the void pointer to the integer pointer by using the
statement given below:
(int*)ptr;
Then, we print the value of the variable which is pointed by the void
pointer 'ptr' by using the statement given below:
*(int*)ptr;
Dynamic Memory Allocation
malloc()
calloc()
realloc()
free()
quick look at the methods used for dynamic memory allocation.
C malloc()
• Syntax of realloc()
free(ptr);
return 0;
}
free()
• Dynamically allocated memory created with either calloc() or malloc()
doesn't get freed on their own.
• Syntax of free()
• free(ptr);
• This statement frees the space allocated in the memory pointed by ptr.
File Handling in C
Why files are needed?
• When a program is terminated, the entire data is lost.
• Storing in a file will preserve your data even if the program
terminates.
• It helps in preserving the data or information for reusability.
• And without loss of data, we can easily transfer the contents of a
file.
File Operations
• File handling in C enables us to create, update, read, and delete the files.
• In C, you can perform four major operations on files,
• Creation of a file
• Opening a file
• Reading a file
• Writing to a file
• Closing a file
Types of Files
• Instead of storing data in plain text, they store it in the binary form
(0's and 1's).
• not readable easily
Functions for file handling:
How to Create a File
A file is nothing but space in a memory where data is stored.
Syntax:
FILE *fp;
fp = fopen ("file_name", "mode");
For example,
fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb");
• fopen is a standard function which is used to open a file.
• If the file is not present on the system, then it is created and then
opened.
• If a file is already present on the system, then it is directly opened
using this function.
• fp is a file pointer which points to the type file.
• Whenever you open or create a file, you have to specify what you
are going to do with the file.
• A file in ‘C’ programming can be created or opened for
reading/writing purposes.
• A mode is used to specify whether you want to open a file for any of
the below-given purposes.
Example
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen ("data.txt", "w");
}
Output:
File is created in the same folder where you have saved your code.
You can specify the path where you want to create your file
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen ("D://data.txt", "w");
}
How to Close a file
The file (both text and binary) should be closed after reading/writing.
fclose (file_pointer);
Example:
FILE *fp;
fp = fopen ("data.txt", "r");
fclose (fp);
Reading and writing to a text file
if(fptr == NULL)
{
printf("Error!");
exit(1);
}
printf("Enter num: ");
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
}
Reading File : fscanf() function
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
return 0;
}
C fputc() and fgetc()
Writing File : fputc() function
The fputc() function is used to write a single character into file.
Example:
#include <stdio.h>
Void main(){
FILE *fp;
fp = fopen("file1.txt", "w");//opening file
fputc('a',fp);//writing single character into file
fclose(fp);//closing file
}
fgetc() function
#include<stdio.h> while((c=fgetc(fp))!=EOF){
void main(){ printf("%c",c);
FILE *fp; }
char c; fclose(fp);
fp=fopen("myfile.txt","r"); getch();
}
C fputs() and fgets()
• The fputs() and fgets() in C programming are used to write and read
string
#include<stdio.h>
void main(){
FILE *fp;
fp=fopen("myfile2.txt","w");
fputs("hello c programming",fp);
fclose(fp);
getch();
}
fgets()
#include<stdio.h>
void main(){
FILE *fp;
char text[300];
fp=fopen("myfile2.txt","r");
printf("%s",fgets(text,200,fp));
fclose(fp);
getch();
}
ftell() function
• The ftell() function returns the current file position of the specified
stream
• Syntax:
• Syntax of fseek()
• fseek(FILE * stream, long int offset, int whence);
• The first parameter stream is the pointer to the file.
• The second parameter is the position of the record to be found.
• third parameter specifies the location where the offset starts.
It can have the following three values
int main()
{
// declaring a file pointer
FILE *filePointer;