PPS Unit-5
PPS Unit-5
Pointers
Pointer is a variable that can store address of other variable of same type.
Address of variable
We can access address of any variable using ampersand &
Example-
If you have a variable v in your program, &v will give you its address in the
memory.
Declaring a pointer
Pointer can be declared with the help of * (astrik sign).
Syntax –
data_type *var_name;
Example-
int *p;
char *q;
Where, * is used to denote that p ,q are pointer variables and not a normal
variable.
int a;
char b;
p=&a; // p stores address of other integer variable
q=&b; // q stores address of other character variable
Initialization of a pointer
We can initialize a pointer by NULL means no address. it is also known as null pointer.
Example-
int *p=NULL;
int *p=&a; //or initialize by by any address
*p means value at
address p. where
Indirection(*) and address (&) p=&a. put value So *p
Indirection(*) and address (&) are inverse of each other
=*&a
Inverse
* & Since * and & are
Example- inverse =*& a.it will
int a=5; print value of a. 5
int *p;
p=&a;
then we can print a with the help of pointer as printf(“%d”,*p);
Pointer to Pointer
Pointers are used to store the address of other variables of similar data type. But if you
want to store the address of a pointer variable, then you again need a pointer to store
it. Thus, when one pointer variable stores the address of another pointer variable, it is
known as Pointer to Pointer variable or Double Pointer. Double pointer can store
address of pointer, triple pointer can store address of double pointer and so on
Example-
int a=5;
int *p; //normal pointer
int **q; // Double Pointer
p=&a;
q=&p;
Void Pointer
A void pointer is a pointer that has no associated data type with it. A void pointer can
store address of variable of any type and can be change to that type.
Example-
int a;
char b;
void *p;
p=&a;
p=&b;
Operation on pointer or pointer arithmetic
Increment /decrement
Addition
Subtraction
Comparison
Multiply and division not allowed in pointer arithmetic
Relationship between array and pointer
Name of the array is base address of array itself. The address of &x[0] and x is the
same. It's because the variable name x points to the first element of the array.
From the above example, it is clear that &x[0] is equivalent to x. so x[0] is equivalent
to *x.
Similarly,
Advantages of pointer
Fast and direct processing
Can save memory
Disadvantages of pointer
Complicate to manage
Security problem
Application of pointer
One of the most important applications of pointer is dynamic memory allocation
Dynamic memory allocation
Dynamic memory allocation refers to managing system memory at runtime using four
functions of <stdlib.h> header file
malloc()
calloc()
realloc()
free().
malloc()
malloc() allocates memory block of specified size in bytes but does not initialize.
The function takes a single argument, which is the size of the memory to be allocated.
Syntax:
pointer = (type*) malloc(byte-size)
Example :
int *ptr;
ptr = (int*) malloc(4); //allocates block of 4 byte
calloc()
calloc() allocates memory block of specified size in bytes and number of block. It
initializes to zero. The function takes a two argument, number of block and size of the
memory to be allocated.
Syntax:
pointer = (type*) calloc(no of block, byte-size)
Example :
int *ptr;
3 PPS NOTES BY PRADEEP KUMAR SHARMA
PPS NOTES BY PRADEEP KUMAR SHARMA
Example:
struct node
{
int data;
struct node *next ; //self referential structure
};
Advantages using linked list
Following operations can be handled easily using linked list
Insertion
Searching
Deletion
File handling in C
File is the collection of information and we can access file using predefined structure
FILE.
Example:
FILE *f;
FILE *f1,f2;
Basic file operations
Opening a file
Reading data from a file
Writing data into a file
Closing a file
Opening a file
To open a file C provide a function fopen() that returns a FILE pointer.
Syntax:
fopen(“file name”, “opening mode”)
where opening mode are-
r - Opens a text file in reading mode if file exists otherwise returns NULL
w - Opens a text file in wirting mode and overwrite existing file
a - Opens a text file in append mode if file exists it add data otherwise create
new file.
Example:
FILE *f;
f=fopen(“abc.txt”,”r”);
Closing a file
To close a file fclose() function is used
Example:
FILE *f;
f=fopen(“abc.txt”,”w”);
fclose(f);
Reading from a file
The reading from a file operation is performed using the following pre-defined file
handling methods.
1. getc() / fgetc()-This function is used to read a character from specified file which
is opened in reading mode.
Syntax: fgetc(FILE *fp)
2. getw()-This function is used to read an integer value form the specified file which
is opened in reading mode.
Syntax: getw(FILE *fp)
5 PPS NOTES BY PRADEEP KUMAR SHARMA
PPS NOTES BY PRADEEP KUMAR SHARMA
3. fscanf()-This function is used to read multiple datatype values from specified file
which is opened in reading mode.
4. fgets()-This method is used for reading a set of characters from a file which is
opened in reading mode
Writing to a file
The writing to a file operation is performed using the following pre-defined file handling
methods.
1. putc() / fputc()-This function is used to write a character to specified file which is
opened in w mode.
Syntax: fputc(char c, FILE *fp)
2. putw()-This function is used to write an integer value to the specified file which is
opened in w mode.
Syntax: putw(int a,FILE *fp)
2. rewind()-This function is used reset the cursor position to the beginning of the
file.
Syntax: rewind( *file_pointer )
3. fseek()-This function is used to set the cursor position to the specific position.
Syntax: fseek( *file_pointer, numberOfCharacters, fromPosition )
Program: to copy content of one file abc.txt to another file
xyz.txt
#include <stdio.h>
int main()
{
FILE *fptr1, *fptr2;
char c;
fptr1 = fopen(abc.txt, "r"); // Open one file for reading
if (fptr1 == NULL)
{
printf("file not fount”);
}
fptr2 = fopen(xyz.txt, "w"); // Open another file for writing
c = fgetc(fptr1); // Read contents from file
while (c != EOF) //EOF means end of file
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied);
fclose(fptr1);
fclose(fptr2);
return 0;
}
C-Preprocessors
This is the first block of the c program. It start with pound sign(#) then we write preprocessor
command that tell the preprocessor how to prepare the program before compilation.
There are three types of preprocessor command-
File inclusion- “include” command is used to attach pre-defined or user defined file. E.g.
#include<stdio.h> angular brackets used for pre defined file or library
#include”xyz.cpp” double quotes are used for user defined file or library
Macro expansion- It is used to define symbolic constant using “define” command. There
are two types of macro
o Simple macro
#define pi 3.14 //defining of macro
printf(“%f”, 2*pi*2) //calling of macro
o Parameterized macro
#define sq(x) x*x //defining of macro
printf(“%d”, 81/sq(9)) //calling of macro that prints 81
Conditional compilation- It is used to include or exclude codes according to condition
using following commands-
#if, #else, #elif, #endif
e.g.
#define a 10
#if a>20
#include”abc.cpp”
#elif a<=10
#include”abc.cpp”
#endif