0% found this document useful (0 votes)
742 views9 pages

PPS Unit-5

Pps unit 5 notes

Uploaded by

dehama9671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
742 views9 pages

PPS Unit-5

Pps unit 5 notes

Uploaded by

dehama9671
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PPS NOTES BY PRADEEP KUMAR SHARMA

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);

1 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

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,

2 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

&x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).


&x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2). so
&x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).

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

ptr = (int*) calloc(10,4); //allocates 10 block of 4 byte each


realloc()
realloc() changes size of memory block of specified new size in bytes. It initializes to
zero. The function takes a two argument, pointer and new size of the memory to be
allocated.
Syntax:
realloc(pointer,new size)
Example :
int *ptr;
realloc(ptr,8);
free()
free() deallocates or deletes the allocated block The function takes a one
argument,pointer
Syntax:
free(pointer)
Example:
int *ptr;
free(ptr);
Self referential structure
Self Referential structures are those structures that have one or more pointers which
point to the same type of structure, as their member. In other words, structures pointing
to the same type of structures are self-referential. Such as linked list

Data next Data next Data next

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

4 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

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.

Syntax: fscanf( *file_pointer, typeSpecifier, &variableName )

4. fgets()-This method is used for reading a set of characters from a file which is
opened in reading mode

Syntax: fgets( variableName, numberOfCharacters, *file_pointer )

5. fread()-This function is used to read specific number of sequence of characters


from the specified file which is opened in reading mode.

Syntax:fread(variable, sizeofReadingElement, numberOfCharacters, FILE *pointer


)

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)

3. fprintf()-This function is used to write multiple datatype values to specified file


which is opened in w mode.

Syntax: fprintf( *file_pointer, typeSpecifier,variables)

4. fputs()-This method is used for writing a set of characters to a file which is


opened in w mode

Syntax: fputs( variableName, *file_pointer )

5. fwrite()-This function is used to write specific number of sequence of characters


to the specified file which is opened in w mode.

Syntax: fwrite( “StringData”,sizeof(char), numberOfCharacters, FILE *pointer )

6 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

Cursor Positioning Functions in Files


C programming language provides various pre-defined methods to set the cursor
position in files. The following are the methods available in c, to position cursor in a file.
1. ftell()-This function returns the current position of the cursor in the file
Syntax: ftell( *file_pointer )

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;
}

7 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

Command line argument


It is a parameter supplied or passed to your program at the time of execution.
Command line argument is an important concept in C programming in which we can
read values without using scanf(). It is mostly used when you need to control your
program from outside. Command line arguments are passed to the main() function.
int main ( int argc, char *argv [ ] )
where argc=argument count and argv=argument value
Program to find the sum of two integer numbers using command
line arguments in C
#include <stdio.h>
int main(int argc, char *argv[])
{
int a,b,sum;
if(argc<3)
{
printf(" less argument"\n");
}
else
{
a = atoi(argv[1]);
b = atoi(argv[2]); //atoi() is used to convert ascii to integer
sum = a+b;
printf("Sum = %d”,sum);
}
return 0;
}
After typing Do following -
 Save, compile and run the program.
 Let we have saved it by name “sum.cpp”
 then open DOS and go to program directory C:\TC\BIN
 type program name and arguments with space
C:\TC\BIN>sum 10 20

argv[0] argv[1] argv[2]


first argument wll be name of program
output :
sum=30

8 PPS NOTES BY PRADEEP KUMAR SHARMA


PPS NOTES BY PRADEEP KUMAR SHARMA

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

9 PPS NOTES BY PRADEEP KUMAR SHARMA

You might also like