SlideShare a Scribd company logo
PAPER: INTRODUCTION PROGRAMMING LANGUAGE USING C
PAPER ID: 20105
PAPER CODE: BCA 105
DR. VARUN TIWARI
(ASSOCIATE PROFESSOR)
(DEPARTMENT OF COMPUTER SCIENCE)
BOSCO TECHNICAL TRAINING SOCIETY,
DON BOSCO TECHNICAL SCHOOL, OKHLA ROAD , NEW DELHI
C POINTERS
OBJECTIVES
IN THIS CHAPTER YOU WILL LEARN:
1. TO UNDERSTAND POINTERS IN C.
2. TO LEARN ABOUT ARRAY & POINTER RELATIONSHIP.
3. TO LEARN ABOUT POINTER ARITHMETIC.
4. TO LEARN ABOUT DYNAMIC MEMORY ALLOCATION.
5. TO LEARN ABOUT POINTER TO ARRAYS.
6. TO LEARN ABOUT ARRAY OF POINTERS.
7. TO LEARN ABOUT POINTERS TO FUNCTIONS.
8. TO LEARN ABOUT ARRAY OF POINTERS TO FUNCTIONS.
POINTERS: POINTER IS VERY POWERFUL TOOL IN C LANGUAGE.POINTER ARE USED FREQUENTLY IN C.
POINTERS(THEY) HAVE A NUMBER OF USEFUL APPLICATION. POINTER VARIABLE HOLD THE ADDRESS WHILE
THE ARRAY VARIABLE CODES THE VALUE. IT IS A VARIBALE THAT STORES ADDRESS OF ANOTHER VARIBALE. IT
POINT TO THE NEXT/PREVIOUS MEMORY LOCATION. THE MAIN MOTIVE USE OF POINTER IS TO SAVE
MEMORY SPACE AND ACHIEVE FASTER EXECUTION TIME.THE DECLARATION TELLS THE C COMPILER TO:
1. RESERVE SPACE IN MEMORY TO HOLD THE INTEGER VALUE
2. ASSOCIATE THE NAME K WITH THIS MEMORY LOCATION.
3. STORE THE VALUE 56 IN THIS LOCATION.
DECLARATION OF POINTERS:
SYN: DATA TYPE (POINTER SYMBOL)* VARIABLE NAME;
EXAMPLE 1:
void main() {
int a;
Int *b; //declaration of pointer
a=10;
b=&a;
printf("%u",b); // print address of b
printf("%u",&a); // print the address of a
printf("%d",a); // print the value of a
printf("%d",*b); // print the value of b }
EXAMPLE 2:
#include<stdio.h>
#include<conio.h>
void main() {
int a; int *b, **c; //declaration of pointer
clrscr();
a=10; b=&a; c=&b;
printf("n %u",&a); // print the address of a
printf("n %u",b); // print the address of b
printf("n %u",*c); // print the address of c
printf("n %u",&b); // print the address of b
printf("n %u",c); // print the address of c
printf("n %d",a); // print the value of a
printf("n %d",*b); // print the value of b
printf("n %d",**c); // print the value of c
getch(); }
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
ARRAY & POINTER RELATIONSHIP:
BOTH ARE CLOSELY RELATED EACH OTHER. AN ARRAY NAME CAN BE THOUGHT OF AS A CONSTANT POINTER. THEY CAN BE USED TO
DO ANY OPERATION INVOLVING ARRAY SUBSCRIPTING.
DECLARATION OF ARRAY & POINTER:
int a[4]; // CREATE AN ARRAY (SIZE IS 4)
int *p; // CREATE P AS A POINTER TYPE VARIABLE
p=a; //PASSING ARRAY IN POINTER TYPE VARIABLE
HERE THE ARRAY NAME A (WITHOUT A SUBSCRIPT) IS A (CONSTANT) POINTER TO THE FIRST ELEMENT OF THE ARRAY, WE CAN SET P
TO THE ADDRESS OF THE FIRST ELEMENT IN ARRAY A WITH THE STATEMENT
THIS IS EQUIVALENT TO TAKING THE ADDRESS OF THE FIRST ELEMENT OF THE ARRAY AS FOLLOWS:
p = &a[0]; // PASSING ADDRESS OF A[0] TO P
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
POINTER ARITHMETIC:
A POINTER IN C IS AN ADDRESS, WHICH IS A NUMERIC VALUE. THEREFORE, YOU CAN PERFORM ARITHMETIC
OPERATIONS ON A POINTER JUST AS YOU CAN ON A NUMERIC VALUE. WE CAN PERFORM POINTER ARITHMETIC IN
DIFFERENT WAYS IN C. THERE ARE FOUR ARITHMETIC OPERATORS THAT CAN BE USED ON POINTERS:
• INCREMENT (++)
• DECREMENT (--)
• ADDITION (+)
• SUBTRACTION (-)
FOR 32-BIT INT VARIABLE, IT WILL BE INCREMENTED BY 2 BYTES.
FOR 64-BIT INT VARIABLE, IT WILL BE INCREMENTED BY 4 BYTES.
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
DYNAMIC MEMORY ALLOCATION:
C DYNAMIC MEMORY ALLOCATION CAN BE DEFINED AS A PROCEDURE IN WHICH THE SIZE OF A DATA
STRUCTURE (LIKE ARRAY) IS CHANGED DURING RUNTIME.
THERE ARE 4 LIBRARY FUNCTIONS PROVIDED BY C DEFINED UNDER <STDLIB.H> HEADER FILE TO FACILAITE
DYNAMIC MEMORY ALLOCATION IN C PROGRAMMING:
1. MALLOC ()
2. CALLOC ()
3. FREE()
4. REALLOC()
C MALLOC()
MALLOC() FUNCTION IS USED TO DYNAMICALLY ALLOCATE A SINGLE LARGE BLOCK OF
MWMORY WITH THE SPECIFIED SIZE. IT RETURNS A POINTER OF TYPE VOID WHICH CAN
BE CAST INTO A POINTER OF ANY FORM. IT INITIALIZED EACH BLOCK WITH DEFAULT
GARBAGE VALUE.
SYNTAX:
PTR=(CAST-TYPE *) MALLOC (BYTE-SIZE)
EXAMPLE:
PTR =(INT *) MALLOC (20*SIZEOF(INT));
(HERE THE SIZE OF INT IS 2 BYTES,THIS STATEMENT WILL ALLOCATE 40 BYTES OF MEMORY.
AND THE POINTER PTR HOLDS THE ADDRESS OF THE FIRST BYTE IN THE ALLOCATED MEMORY.
EXAMPLE:
Pointers in C and Dynamic Memory Allocation
C CALLOC()
“CALLOC” OR “CONTIGUOUS ALLOCATION” METHOD IN C IS USED TO DYNAMICALLY
ALLOCATE THE SPECIFIED NUMBER OF BLOCKS OF MEMORY OF THE SPECIFIED TYPE. IT
INITIALIZES EACH BLOCK WITH A DEFAULT VALUE ‘0’.
SYNTAX:
P = (int*)calloc(n, Element Size);
EXAMPLE:
PTR = (INT*) CALLOC(10, SIZEOF(FLOAT));
(HERE THE SIZE OF INT IS 2 BYTES, THIS STATEMENT WILL ALLOCATE 20 BYTES OF MEMORY.
AND THE POINTER PTR HOLDS THE ADDRESS OF THE FIRST BYTE IN THE ALLOCATED MEMORY.
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
C REALLOC()
“REALLOC()” FUNCTION IS USED TO REALLOCATE A MEMORY
DYNAMICALLY WHICH HAVE ALREADY CREATED BY MALLOC () AND
CALLOC() FUNCTION.
SYNTAX:
P = (int*) realloc(pointer variable, New size);
EXAMPLE:
P = (INT*) REALLOC(P, 7);
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
FREE()
“FREE()” FUNCTION IS USED TO RELEASE MEMORY WHICH IS CREATED BY
MALLOC(), CALLOC() AND REALLOC() FUNCTION.
SYNTAX:
Free(pointer variable);
EXAMPLE:
FREE(P);
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
POINTER TO ARRAY()
Pointers in C and Dynamic Memory Allocation
ARRAY OF POINTERS()
Pointers in C and Dynamic Memory Allocation
POINTERS TO FUNCTIONS()
Pointers in C and Dynamic Memory Allocation
ARRAY OF POINTERS TO FUNCTIONS()
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
Pointers in C and Dynamic Memory Allocation
THANK YOU
Ad

More Related Content

What's hot (20)

Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
Dr. SURBHI SAROHA
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
Dr. SURBHI SAROHA
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
Md. Imran Hossain Showrov
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
Ritika Sharma
 
C語言基本資料型別與變數
C語言基本資料型別與變數C語言基本資料型別與變數
C語言基本資料型別與變數
吳錫修 (ShyiShiou Wu)
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
Dr. SURBHI SAROHA
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
MomenMostafa
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
Md. Imran Hossain Showrov
 
Function recap
Function recapFunction recap
Function recap
alish sha
 
Input output functions
Input output functionsInput output functions
Input output functions
hyderali123
 
Pointer and polymorphism
Pointer and polymorphismPointer and polymorphism
Pointer and polymorphism
SangeethaSasi1
 
8.2
8.28.2
8.2
namthip2539
 
Diff between c and c++
Diff between c and c++Diff between c and c++
Diff between c and c++
VishrudhMamidala
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
Rumman Ansari
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
Hitesh Kumar
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
Icaii Infotech
 
C Programming Assignment
C Programming AssignmentC Programming Assignment
C Programming Assignment
Vijayananda Mohire
 
Maintainable go
Maintainable goMaintainable go
Maintainable go
Yu-Shuan Hsieh
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4
Hazwan Arif
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
argusacademy
 

Similar to Pointers in C and Dynamic Memory Allocation (20)

88 c-programs
88 c-programs88 c-programs
88 c-programs
Leandro Schenone
 
Ticket window & automation system
Ticket window & automation systemTicket window & automation system
Ticket window & automation system
Upendra Sengar
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
Kumaran K
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
Saifur Rahman
 
Programming in C - interview questions.pdf
Programming in C - interview questions.pdfProgramming in C - interview questions.pdf
Programming in C - interview questions.pdf
SergiuMatei7
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. Gholkar
PRAVIN GHOLKAR
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
Syed Zaid Irshad
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath
 
C language updated
C language updatedC language updated
C language updated
Arafat Bin Reza
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
Minh Thắng Trần
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
Sumit Saini
 
Function in c
Function in cFunction in c
Function in c
savitamhaske
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
Vikram Nandini
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
Session 2 - Objective-C basics
Session 2 - Objective-C basicsSession 2 - Objective-C basics
Session 2 - Objective-C basics
Vu Tran Lam
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Syed Mustafa
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
vanshhans21102005
 
23_2-Pointer and DMA.pptx
23_2-Pointer and DMA.pptx23_2-Pointer and DMA.pptx
23_2-Pointer and DMA.pptx
GandavadiVenkatesh1
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointers
kirthika jeyenth
 
Ticket window & automation system
Ticket window & automation systemTicket window & automation system
Ticket window & automation system
Upendra Sengar
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
Kumaran K
 
C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)C cheat sheet for varsity (extreme edition)
C cheat sheet for varsity (extreme edition)
Saifur Rahman
 
Programming in C - interview questions.pdf
Programming in C - interview questions.pdfProgramming in C - interview questions.pdf
Programming in C - interview questions.pdf
SergiuMatei7
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. Gholkar
PRAVIN GHOLKAR
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
Syed Zaid Irshad
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
Sumit Saini
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
kushwahashivam413
 
Session 2 - Objective-C basics
Session 2 - Objective-C basicsSession 2 - Objective-C basics
Session 2 - Objective-C basics
Vu Tran Lam
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Syed Mustafa
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
vanshhans21102005
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointers
kirthika jeyenth
 
Ad

More from Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi) (20)

Preprocessor Directive in C
Preprocessor Directive in CPreprocessor Directive in C
Preprocessor Directive in C
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Bit field enum and command line arguments
Bit field enum and command line argumentsBit field enum and command line arguments
Bit field enum and command line arguments
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Array in C
Array in CArray in C
Array in C
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
C storage class
C storage classC storage class
C storage class
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
C Operators
C OperatorsC Operators
C Operators
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
C programming Basics
C programming BasicsC programming Basics
C programming Basics
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Software Development Skills and SDLC
Software Development Skills and SDLCSoftware Development Skills and SDLC
Software Development Skills and SDLC
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Mobile commerce
Mobile commerceMobile commerce
Mobile commerce
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
E commerce application
E commerce applicationE commerce application
E commerce application
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Data normalization
Data normalizationData normalization
Data normalization
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Html Form Controls
Html Form ControlsHtml Form Controls
Html Form Controls
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Security issue in e commerce
Security issue in e commerceSecurity issue in e commerce
Security issue in e commerce
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
ER to Relational Mapping
ER to Relational MappingER to Relational Mapping
ER to Relational Mapping
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Entity Relationship Model
Entity Relationship ModelEntity Relationship Model
Entity Relationship Model
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Database connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwariDatabase connectivity with data reader by varun tiwari
Database connectivity with data reader by varun tiwari
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Ado vs ado.net by varun tiwari
Ado vs ado.net by varun tiwariAdo vs ado.net by varun tiwari
Ado vs ado.net by varun tiwari
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Client server architecture in .net by varun tiwari
Client server architecture in .net by varun tiwariClient server architecture in .net by varun tiwari
Client server architecture in .net by varun tiwari
Bosco Technical Training Society, Don Bosco Technical School (Aff. GGSIP University, New Delhi)
 
Ad

Recently uploaded (20)

spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 

Pointers in C and Dynamic Memory Allocation

  • 1. PAPER: INTRODUCTION PROGRAMMING LANGUAGE USING C PAPER ID: 20105 PAPER CODE: BCA 105 DR. VARUN TIWARI (ASSOCIATE PROFESSOR) (DEPARTMENT OF COMPUTER SCIENCE) BOSCO TECHNICAL TRAINING SOCIETY, DON BOSCO TECHNICAL SCHOOL, OKHLA ROAD , NEW DELHI
  • 3. OBJECTIVES IN THIS CHAPTER YOU WILL LEARN: 1. TO UNDERSTAND POINTERS IN C. 2. TO LEARN ABOUT ARRAY & POINTER RELATIONSHIP. 3. TO LEARN ABOUT POINTER ARITHMETIC. 4. TO LEARN ABOUT DYNAMIC MEMORY ALLOCATION. 5. TO LEARN ABOUT POINTER TO ARRAYS. 6. TO LEARN ABOUT ARRAY OF POINTERS. 7. TO LEARN ABOUT POINTERS TO FUNCTIONS. 8. TO LEARN ABOUT ARRAY OF POINTERS TO FUNCTIONS.
  • 4. POINTERS: POINTER IS VERY POWERFUL TOOL IN C LANGUAGE.POINTER ARE USED FREQUENTLY IN C. POINTERS(THEY) HAVE A NUMBER OF USEFUL APPLICATION. POINTER VARIABLE HOLD THE ADDRESS WHILE THE ARRAY VARIABLE CODES THE VALUE. IT IS A VARIBALE THAT STORES ADDRESS OF ANOTHER VARIBALE. IT POINT TO THE NEXT/PREVIOUS MEMORY LOCATION. THE MAIN MOTIVE USE OF POINTER IS TO SAVE MEMORY SPACE AND ACHIEVE FASTER EXECUTION TIME.THE DECLARATION TELLS THE C COMPILER TO: 1. RESERVE SPACE IN MEMORY TO HOLD THE INTEGER VALUE 2. ASSOCIATE THE NAME K WITH THIS MEMORY LOCATION. 3. STORE THE VALUE 56 IN THIS LOCATION.
  • 5. DECLARATION OF POINTERS: SYN: DATA TYPE (POINTER SYMBOL)* VARIABLE NAME; EXAMPLE 1: void main() { int a; Int *b; //declaration of pointer a=10; b=&a; printf("%u",b); // print address of b printf("%u",&a); // print the address of a printf("%d",a); // print the value of a printf("%d",*b); // print the value of b }
  • 6. EXAMPLE 2: #include<stdio.h> #include<conio.h> void main() { int a; int *b, **c; //declaration of pointer clrscr(); a=10; b=&a; c=&b; printf("n %u",&a); // print the address of a printf("n %u",b); // print the address of b printf("n %u",*c); // print the address of c printf("n %u",&b); // print the address of b printf("n %u",c); // print the address of c printf("n %d",a); // print the value of a printf("n %d",*b); // print the value of b printf("n %d",**c); // print the value of c getch(); }
  • 9. ARRAY & POINTER RELATIONSHIP: BOTH ARE CLOSELY RELATED EACH OTHER. AN ARRAY NAME CAN BE THOUGHT OF AS A CONSTANT POINTER. THEY CAN BE USED TO DO ANY OPERATION INVOLVING ARRAY SUBSCRIPTING. DECLARATION OF ARRAY & POINTER: int a[4]; // CREATE AN ARRAY (SIZE IS 4) int *p; // CREATE P AS A POINTER TYPE VARIABLE p=a; //PASSING ARRAY IN POINTER TYPE VARIABLE HERE THE ARRAY NAME A (WITHOUT A SUBSCRIPT) IS A (CONSTANT) POINTER TO THE FIRST ELEMENT OF THE ARRAY, WE CAN SET P TO THE ADDRESS OF THE FIRST ELEMENT IN ARRAY A WITH THE STATEMENT THIS IS EQUIVALENT TO TAKING THE ADDRESS OF THE FIRST ELEMENT OF THE ARRAY AS FOLLOWS: p = &a[0]; // PASSING ADDRESS OF A[0] TO P
  • 20. POINTER ARITHMETIC: A POINTER IN C IS AN ADDRESS, WHICH IS A NUMERIC VALUE. THEREFORE, YOU CAN PERFORM ARITHMETIC OPERATIONS ON A POINTER JUST AS YOU CAN ON A NUMERIC VALUE. WE CAN PERFORM POINTER ARITHMETIC IN DIFFERENT WAYS IN C. THERE ARE FOUR ARITHMETIC OPERATORS THAT CAN BE USED ON POINTERS: • INCREMENT (++) • DECREMENT (--) • ADDITION (+) • SUBTRACTION (-) FOR 32-BIT INT VARIABLE, IT WILL BE INCREMENTED BY 2 BYTES. FOR 64-BIT INT VARIABLE, IT WILL BE INCREMENTED BY 4 BYTES.
  • 23. DYNAMIC MEMORY ALLOCATION: C DYNAMIC MEMORY ALLOCATION CAN BE DEFINED AS A PROCEDURE IN WHICH THE SIZE OF A DATA STRUCTURE (LIKE ARRAY) IS CHANGED DURING RUNTIME. THERE ARE 4 LIBRARY FUNCTIONS PROVIDED BY C DEFINED UNDER <STDLIB.H> HEADER FILE TO FACILAITE DYNAMIC MEMORY ALLOCATION IN C PROGRAMMING: 1. MALLOC () 2. CALLOC () 3. FREE() 4. REALLOC()
  • 24. C MALLOC() MALLOC() FUNCTION IS USED TO DYNAMICALLY ALLOCATE A SINGLE LARGE BLOCK OF MWMORY WITH THE SPECIFIED SIZE. IT RETURNS A POINTER OF TYPE VOID WHICH CAN BE CAST INTO A POINTER OF ANY FORM. IT INITIALIZED EACH BLOCK WITH DEFAULT GARBAGE VALUE. SYNTAX: PTR=(CAST-TYPE *) MALLOC (BYTE-SIZE) EXAMPLE: PTR =(INT *) MALLOC (20*SIZEOF(INT)); (HERE THE SIZE OF INT IS 2 BYTES,THIS STATEMENT WILL ALLOCATE 40 BYTES OF MEMORY. AND THE POINTER PTR HOLDS THE ADDRESS OF THE FIRST BYTE IN THE ALLOCATED MEMORY.
  • 27. C CALLOC() “CALLOC” OR “CONTIGUOUS ALLOCATION” METHOD IN C IS USED TO DYNAMICALLY ALLOCATE THE SPECIFIED NUMBER OF BLOCKS OF MEMORY OF THE SPECIFIED TYPE. IT INITIALIZES EACH BLOCK WITH A DEFAULT VALUE ‘0’. SYNTAX: P = (int*)calloc(n, Element Size); EXAMPLE: PTR = (INT*) CALLOC(10, SIZEOF(FLOAT)); (HERE THE SIZE OF INT IS 2 BYTES, THIS STATEMENT WILL ALLOCATE 20 BYTES OF MEMORY. AND THE POINTER PTR HOLDS THE ADDRESS OF THE FIRST BYTE IN THE ALLOCATED MEMORY.
  • 30. C REALLOC() “REALLOC()” FUNCTION IS USED TO REALLOCATE A MEMORY DYNAMICALLY WHICH HAVE ALREADY CREATED BY MALLOC () AND CALLOC() FUNCTION. SYNTAX: P = (int*) realloc(pointer variable, New size); EXAMPLE: P = (INT*) REALLOC(P, 7);
  • 33. FREE() “FREE()” FUNCTION IS USED TO RELEASE MEMORY WHICH IS CREATED BY MALLOC(), CALLOC() AND REALLOC() FUNCTION. SYNTAX: Free(pointer variable); EXAMPLE: FREE(P);
  • 42. ARRAY OF POINTERS TO FUNCTIONS()
  翻译: