SlideShare a Scribd company logo
1 of 46
Download to read offline
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(); }
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
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.
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:
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.
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);
FREE()
“FREE()” FUNCTION IS USED TO RELEASE MEMORY WHICH IS CREATED BY
MALLOC(), CALLOC() AND REALLOC() FUNCTION.
SYNTAX:
Free(pointer variable);
EXAMPLE:
FREE(P);
POINTER TO ARRAY()
ARRAY OF POINTERS()
POINTERS TO FUNCTIONS()
ARRAY OF POINTERS TO FUNCTIONS()
THANK YOU

More Related Content

What's hot

What's hot (20)

Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
C語言基本資料型別與變數
C語言基本資料型別與變數C語言基本資料型別與變數
C語言基本資料型別與變數
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Lecture 8- Data Input and Output
Lecture 8- Data Input and OutputLecture 8- Data Input and Output
Lecture 8- Data Input and Output
 
Function recap
Function recapFunction recap
Function recap
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Pointer and polymorphism
Pointer and polymorphismPointer and polymorphism
Pointer and polymorphism
 
8.2
8.28.2
8.2
 
Diff between c and c++
Diff between c and c++Diff between c and c++
Diff between c and c++
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
Ternary operator
Ternary operatorTernary operator
Ternary operator
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
C Programming Assignment
C Programming AssignmentC Programming Assignment
C Programming Assignment
 
Maintainable go
Maintainable goMaintainable go
Maintainable go
 
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
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
 

Similar to Pointers in C and Dynamic Memory Allocation

Ticket window & automation system
Ticket window & automation systemTicket window & automation system
Ticket window & automation systemUpendra Sengar
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&aKumaran 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.pdfSergiuMatei7
 
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. GholkarPRAVIN GHOLKAR
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xiiSyed Zaid Irshad
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184Sumit 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 basicsVu Tran Lam
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin 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 CSyed Mustafa
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxvanshhans21102005
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointerskirthika jeyenth
 

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

88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Ticket window & automation system
Ticket window & automation systemTicket window & automation system
Ticket window & automation system
 
C,c++ interview q&a
C,c++ interview q&aC,c++ interview q&a
C,c++ interview q&a
 
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)
 
Programming in C - interview questions.pdf
Programming in C - interview questions.pdfProgramming in C - interview questions.pdf
Programming in C - interview questions.pdf
 
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
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
C language updated
C language updatedC language updated
C language updated
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Function in c
Function in cFunction in c
Function in c
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
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...
 
Session 2 - Objective-C basics
Session 2 - Objective-C basicsSession 2 - Objective-C basics
Session 2 - Objective-C basics
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
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
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
23_2-Pointer and DMA.pptx
23_2-Pointer and DMA.pptx23_2-Pointer and DMA.pptx
23_2-Pointer and DMA.pptx
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointers
 

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

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
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 
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
 
Array in C
Array in CArray in C
Array in C
 
C storage class
C storage classC storage class
C storage class
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
 
C Operators
C OperatorsC Operators
C Operators
 
C programming Basics
C programming BasicsC programming Basics
C programming Basics
 
Software Development Skills and SDLC
Software Development Skills and SDLCSoftware Development Skills and SDLC
Software Development Skills and SDLC
 
Mobile commerce
Mobile commerceMobile commerce
Mobile commerce
 
E commerce application
E commerce applicationE commerce application
E commerce application
 
Data normalization
Data normalizationData normalization
Data normalization
 
Html Form Controls
Html Form ControlsHtml Form Controls
Html Form Controls
 
Security issue in e commerce
Security issue in e commerceSecurity issue in e commerce
Security issue in e commerce
 
ER to Relational Mapping
ER to Relational MappingER to Relational Mapping
ER to Relational Mapping
 
Entity Relationship Model
Entity Relationship ModelEntity Relationship Model
Entity Relationship Model
 
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
 
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
 
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
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

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(); }
  • 7.
  • 8.
  • 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
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 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.
  • 21.
  • 22.
  • 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.
  • 26.
  • 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.
  • 28.
  • 29.
  • 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);
  • 31.
  • 32.
  • 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);
  • 34.
  • 35.
  • 37.
  • 39.
  • 41.
  • 42. ARRAY OF POINTERS TO FUNCTIONS()
  • 43.
  • 44.
  • 45.