SlideShare a Scribd company logo
1 of 12
POINTERS WITH FUNCTION
POINTERS
Definition
• variable that stores the memory address of another variable.
• directly refers to the location of a value in memory.
Used for
• dynamic memory allocation
• efficient manipulation of data.
• implementation of various advanced data structures and
algorithms.
BASICS OF POINTERS
• Syntax
Data_type *pointer_variable_name;
• Example:
Declaration
int *intPointer;
Initialization
int variable = 10;
int *intPointer = &variable;
• * -> Dereference operator.
POINTER ARITHMETIC
• manipulating the memory addresses stored in pointers.
• navigating through arrays, structures, and dynamic memory.
Increment (`++`) and Decrement (`--`).
• Example
• int numbers[] = {10, 20, 30, 40, 50};
• int *ptr = numbers; // Points to the first element of the array
• Pointer Increment:
• ptr++;
• Pointer Decrement
• ptr--;
PASSING POINTERS TO FUNCTIONS
• Syntax
Return_type function_name(data_type *parameter_name) {
//function body
}
• Example
Void modifyValue(int *ptr) {
*ptr = 20;
}
• Advantages
efficient memory usage
Direct Modification of Original Data
Reduced Overhead
Dynamic Memory Manipulation
RETURNING POINTERS FROM FUNCTIONS
• Syntax
Data_type* function_name() {
// Allocate memory and return a pointer to it
}
• Example
int* allocateAndReturn() {
int *ptr = (int*)malloc(sizeof(int));
// Additional logic or data initialization if needed
return ptr;
}
DYNAMIC MEMORY ALLOCATION
• Malloc (Memory Allocation)
• It is used to dynamically allocate a specified number of bytes of memory.
• Returns a pointer to the beginning of the allocated memory.
Example: Allocating memory for an integer
int *ptr = (int*)malloc(sizeof(int));
• Calloc (Contiguous Allocation)
• contiguous memory allocation and initializes the allocated memory to zero.
• Takes two arguments – the number of elements and the size of each element.
• Returns a pointer to the beginning of the allocated memory.
// Example: Allocating memory for an array of 5 integers
int *arr = (int*)calloc(5, sizeof(int));
• Free
• free is used to deallocate memory previously allocated using malloc or calloc.
• It takes a pointer to the memory block to be freed.
• After freeing memory, the pointer should not be used until reallocated.
// Example: Freeing dynamically allocated memory
free(ptr);
ARRAYS AND POINTERS
• Array Name as a Pointer:
int numbers[] = {10, 20, 30};
int *ptr = numbers; // Equivalent to &numbers[0]
• Pointer Arithmetic with Arrays & Array Indexing with Pointers:
int firstElement = *ptr; // 10
int secondElement = *(ptr + 1); // 20
• Advantages
Dynamic Array Sizes
Efficient Function Arguments
Flexibility in Memory Manipulation
FUNCTION POINTERS
• Definition
. Point to functions instead of data
. Call functions indirectly through the pointer.
• Declaration
int (*functionPtr)(int, int);
• Initialization
int add(int a, int b) {
return a + b;
}
int (*functionPtr)(int, int) = add;
REAL WORLD EXAMPLES OF POINTERS IN FUNCTIONS
• Dynamic Memory Allocation
malloc, calloc, and realloc return pointers to dynamically allocated memory.
• String Manipulation
strcpy, strcat, and strlen use pointers to manipulate strings efficiently.
• Sorting Algorithms
Quicksort or mergesort often use function pointers
• Function Pointers in API Libraries:
APIs often use function pointers to define plugin interfaces
• Memory Management in Data Structures
Data structures like linked lists, trees, and graphs often use function pointers
EXAMPLE PROGRAM
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
printf(“Before swapping: x = %d, y = %dn”, x, y);
swap(&x, &y);
printf(“After swapping: x = %d, y = %dn”, x, y);
return 0;
}

More Related Content

Similar to pointer_in_c_programming_structure and uses.pptx

Similar to pointer_in_c_programming_structure and uses.pptx (20)

C Programming - Refresher - Part III
C Programming - Refresher - Part IIIC Programming - Refresher - Part III
C Programming - Refresher - Part III
 
C96e1 session3 c++
C96e1 session3 c++C96e1 session3 c++
C96e1 session3 c++
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
 
C pointers
C pointersC pointers
C pointers
 
Session 5
Session 5Session 5
Session 5
 
Programming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explainedProgramming fundamentals 2:pointers in c++ clearly explained
Programming fundamentals 2:pointers in c++ clearly explained
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
 
Pointers
PointersPointers
Pointers
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
pointers (1).ppt
pointers (1).pptpointers (1).ppt
pointers (1).ppt
 
Pointers
PointersPointers
Pointers
 
Pointers - DataStructures
Pointers - DataStructuresPointers - DataStructures
Pointers - DataStructures
 
Pointer and polymorphism
Pointer and polymorphismPointer and polymorphism
Pointer and polymorphism
 
Chp3(pointers ref)
Chp3(pointers ref)Chp3(pointers ref)
Chp3(pointers ref)
 
Pointer
PointerPointer
Pointer
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
Data Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptxData Structure & aaplications_Module-1.pptx
Data Structure & aaplications_Module-1.pptx
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 

Recently uploaded

Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxNikitaBankoti2
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 

Recently uploaded (20)

Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 

pointer_in_c_programming_structure and uses.pptx

  • 2. POINTERS Definition • variable that stores the memory address of another variable. • directly refers to the location of a value in memory. Used for • dynamic memory allocation • efficient manipulation of data. • implementation of various advanced data structures and algorithms.
  • 3. BASICS OF POINTERS • Syntax Data_type *pointer_variable_name; • Example: Declaration int *intPointer; Initialization int variable = 10; int *intPointer = &variable; • * -> Dereference operator.
  • 4. POINTER ARITHMETIC • manipulating the memory addresses stored in pointers. • navigating through arrays, structures, and dynamic memory. Increment (`++`) and Decrement (`--`). • Example • int numbers[] = {10, 20, 30, 40, 50}; • int *ptr = numbers; // Points to the first element of the array • Pointer Increment: • ptr++; • Pointer Decrement • ptr--;
  • 5. PASSING POINTERS TO FUNCTIONS • Syntax Return_type function_name(data_type *parameter_name) { //function body } • Example Void modifyValue(int *ptr) { *ptr = 20; } • Advantages efficient memory usage Direct Modification of Original Data Reduced Overhead Dynamic Memory Manipulation
  • 6. RETURNING POINTERS FROM FUNCTIONS • Syntax Data_type* function_name() { // Allocate memory and return a pointer to it } • Example int* allocateAndReturn() { int *ptr = (int*)malloc(sizeof(int)); // Additional logic or data initialization if needed return ptr; }
  • 7. DYNAMIC MEMORY ALLOCATION • Malloc (Memory Allocation) • It is used to dynamically allocate a specified number of bytes of memory. • Returns a pointer to the beginning of the allocated memory. Example: Allocating memory for an integer int *ptr = (int*)malloc(sizeof(int)); • Calloc (Contiguous Allocation) • contiguous memory allocation and initializes the allocated memory to zero. • Takes two arguments – the number of elements and the size of each element. • Returns a pointer to the beginning of the allocated memory. // Example: Allocating memory for an array of 5 integers int *arr = (int*)calloc(5, sizeof(int));
  • 8. • Free • free is used to deallocate memory previously allocated using malloc or calloc. • It takes a pointer to the memory block to be freed. • After freeing memory, the pointer should not be used until reallocated. // Example: Freeing dynamically allocated memory free(ptr);
  • 9. ARRAYS AND POINTERS • Array Name as a Pointer: int numbers[] = {10, 20, 30}; int *ptr = numbers; // Equivalent to &numbers[0] • Pointer Arithmetic with Arrays & Array Indexing with Pointers: int firstElement = *ptr; // 10 int secondElement = *(ptr + 1); // 20 • Advantages Dynamic Array Sizes Efficient Function Arguments Flexibility in Memory Manipulation
  • 10. FUNCTION POINTERS • Definition . Point to functions instead of data . Call functions indirectly through the pointer. • Declaration int (*functionPtr)(int, int); • Initialization int add(int a, int b) { return a + b; } int (*functionPtr)(int, int) = add;
  • 11. REAL WORLD EXAMPLES OF POINTERS IN FUNCTIONS • Dynamic Memory Allocation malloc, calloc, and realloc return pointers to dynamically allocated memory. • String Manipulation strcpy, strcat, and strlen use pointers to manipulate strings efficiently. • Sorting Algorithms Quicksort or mergesort often use function pointers • Function Pointers in API Libraries: APIs often use function pointers to define plugin interfaces • Memory Management in Data Structures Data structures like linked lists, trees, and graphs often use function pointers
  • 12. EXAMPLE PROGRAM #include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 5, y = 10; printf(“Before swapping: x = %d, y = %dn”, x, y); swap(&x, &y); printf(“After swapping: x = %d, y = %dn”, x, y); return 0; }