SlideShare a Scribd company logo

Pointers.pptx

MEMORY ACCESS AND POINTERS​ POINTER BASICS ​ DECLARING​ INITIALIZING​ DEREFERENCING A POINTER​ https://onlinegdb.com/bPQWOMG11j https://onlinegdb.com/M6OpGSLSI VOID POINTER: https://onlinegdb.com/zkSslA6Su NULL POINTER: https://onlinegdb.com/GzKIgEel1 PARAMETER PASSING MECHANISM​ https://onlinegdb.com/MJYCrZAnW https://onlinegdb.com/MJYCrZAnW FUNCTION POINTER: https://onlinegdb.com/68rJKtJuq OPERATIONS ON POINTER: https://onlinegdb.com/722tWada6 https://onlinegdb.com/LorgQes_t https://onlinegdb.com/722tWada6

1 of 32
UNIT
III
POINTERS AND STRING
P.ANANTHI, Assistant Professor, Kongu Engineering
College
1
TOPICS: POINTER
• MEMORY ACCESS AND POINTERS
• POINTER BASICS
• DECLARING
• INITIALIZING
• DEREFERENCING A POINTER
• PARAMETER PASSING MECHANISM
• OPERATIONS ON POINTER
2
P.ANANTHI, Assistant Professor, Kongu Engineering
College
MEMORY
ACCESS
Memory access refers to
the ability to read from or
write to specific locations
in the computer's memory.
The memory in a computer
is organized into a
sequence of bytes, each
with a unique address.
3
P.ANANTHI, Assistant Professor, Kongu Engineering
College
MEMORY ACCESS
• Every variable is a memory
location and every memory
location has its address defined
which can be accessed through
ampersand (&) operator, which
denotes an address in memory
4
P.ANANTHI, Assistant Professor, Kongu Engineering
College
5
P.ANANTHI, Assistant Professor, Kongu Engineering
College
Address of a:
0x7ffd7218d764
Address of b:
0x7ffd7218d760
Address of c:
0x7ffd7218d75f
#include <stdio.h>
int main()
{
int a = 10;
double b = 20.5;
char c = 'A';
printf("Address of a: %pn", &a);
printf("Address of b: %pn", &b);
printf("Address of c: %pn", &c);
return 0;
}
6
P.ANANTHI, Assistant Professor, Kongu Engineering
College

More Related Content

What's hot

Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocationvaani pathak
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7sumitbardhan
 
File handling in c
File handling in cFile handling in c
File handling in caakanksha s
 
Linguagem C 05 Vetores E Matrizes
Linguagem C 05 Vetores E MatrizesLinguagem C 05 Vetores E Matrizes
Linguagem C 05 Vetores E MatrizesRegis Magalhães
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c languagegourav kottawar
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++sandeep54552
 
Estrutura de dados - Implementação de filas com listas
Estrutura de dados - Implementação de filas com listasEstrutura de dados - Implementação de filas com listas
Estrutura de dados - Implementação de filas com listasAdriano Teixeira de Souza
 
Structure in c language
Structure in c languageStructure in c language
Structure in c languagesangrampatil81
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template LibraryKumar Gaurav
 
Estrutura de Dados - Aula 04 - Listas Estáticas
Estrutura de Dados - Aula 04 - Listas EstáticasEstrutura de Dados - Aula 04 - Listas Estáticas
Estrutura de Dados - Aula 04 - Listas EstáticasLeinylson Fontinele
 
Pengantar Sistem Berkas
Pengantar Sistem BerkasPengantar Sistem Berkas
Pengantar Sistem BerkasAnggi DHARMA
 

What's hot (20)

Pointers in C
Pointers in CPointers in C
Pointers in C
 
Dynamic Memory Allocation
Dynamic Memory AllocationDynamic Memory Allocation
Dynamic Memory Allocation
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
File in c
File in cFile in c
File in c
 
Structure and union
Structure and unionStructure and union
Structure and union
 
File handling in c
File handling in cFile handling in c
File handling in c
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Linguagem C 05 Vetores E Matrizes
Linguagem C 05 Vetores E MatrizesLinguagem C 05 Vetores E Matrizes
Linguagem C 05 Vetores E Matrizes
 
Programando com Python
Programando com PythonProgramando com Python
Programando com Python
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Estrutura de dados - Implementação de filas com listas
Estrutura de dados - Implementação de filas com listasEstrutura de dados - Implementação de filas com listas
Estrutura de dados - Implementação de filas com listas
 
file
filefile
file
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
File handling in C
File handling in CFile handling in C
File handling in C
 
Estrutura de Dados - Aula 04 - Listas Estáticas
Estrutura de Dados - Aula 04 - Listas EstáticasEstrutura de Dados - Aula 04 - Listas Estáticas
Estrutura de Dados - Aula 04 - Listas Estáticas
 
Pengantar Sistem Berkas
Pengantar Sistem BerkasPengantar Sistem Berkas
Pengantar Sistem Berkas
 

Similar to Pointers.pptx

Similar to Pointers.pptx (20)

Pointers
PointersPointers
Pointers
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Pointers.pdf
Pointers.pdfPointers.pdf
Pointers.pdf
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
 
Pointers
PointersPointers
Pointers
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 

Recently uploaded

MAXIMUM POWER POINT TRACKING ALGORITHMS APPLIED TO WIND-SOLAR HYBRID SYSTEM
MAXIMUM POWER POINT TRACKING ALGORITHMS APPLIED TO WIND-SOLAR HYBRID SYSTEMMAXIMUM POWER POINT TRACKING ALGORITHMS APPLIED TO WIND-SOLAR HYBRID SYSTEM
MAXIMUM POWER POINT TRACKING ALGORITHMS APPLIED TO WIND-SOLAR HYBRID SYSTEMArunkumar Tulasi
 
Nexus - Final Day 12th February 2024.pptx
Nexus - Final Day 12th February 2024.pptxNexus - Final Day 12th February 2024.pptx
Nexus - Final Day 12th February 2024.pptxRohanAgarwal340656
 
Microstrip Bandpass Filter Design using EDA Tolol such as keysight ADS and An...
Microstrip Bandpass Filter Design using EDA Tolol such as keysight ADS and An...Microstrip Bandpass Filter Design using EDA Tolol such as keysight ADS and An...
Microstrip Bandpass Filter Design using EDA Tolol such as keysight ADS and An...GauravBhartie
 
Introduction about Technology roadmap for Industry 4.0
Introduction about Technology roadmap for Industry 4.0Introduction about Technology roadmap for Industry 4.0
Introduction about Technology roadmap for Industry 4.0RaishKhanji
 
Gate-Level Simulation Methodology Improving Gate-Level Simulation Performance
Gate-Level Simulation Methodology Improving Gate-Level Simulation PerformanceGate-Level Simulation Methodology Improving Gate-Level Simulation Performance
Gate-Level Simulation Methodology Improving Gate-Level Simulation Performancesuddentrike2
 
Introduction to the telecom tower industry
Introduction to the telecom tower industryIntroduction to the telecom tower industry
Introduction to the telecom tower industryssuserf5bbfd
 
GDSC solution challenge Android ppt.pptx
GDSC solution challenge Android ppt.pptxGDSC solution challenge Android ppt.pptx
GDSC solution challenge Android ppt.pptxAnandMenon54
 
CCNA: Routing and Switching Fundamentals
CCNA: Routing and Switching FundamentalsCCNA: Routing and Switching Fundamentals
CCNA: Routing and Switching FundamentalsDebabrata Halder
 
Get start with Machine Learning and Vertexai
Get start with Machine Learning and VertexaiGet start with Machine Learning and Vertexai
Get start with Machine Learning and VertexaiAshishChanchal1
 
Architectural Preservation - Heritage, focused on Saudi Arabia
Architectural Preservation - Heritage, focused on Saudi ArabiaArchitectural Preservation - Heritage, focused on Saudi Arabia
Architectural Preservation - Heritage, focused on Saudi ArabiaIgnacio J. Palma, Arch PhD.
 
21 SCHEME_21EC53_VTU_MODULE-4_COMPUTER COMMUNCATION NETWORK.pdf
21 SCHEME_21EC53_VTU_MODULE-4_COMPUTER COMMUNCATION NETWORK.pdf21 SCHEME_21EC53_VTU_MODULE-4_COMPUTER COMMUNCATION NETWORK.pdf
21 SCHEME_21EC53_VTU_MODULE-4_COMPUTER COMMUNCATION NETWORK.pdfDr. Shivashankar
 
INTERACTIVE AQUATIC MUSEUM AT BAGH IBN QASIM CLIFTON KARACHI
INTERACTIVE AQUATIC MUSEUM AT BAGH IBN QASIM CLIFTON KARACHIINTERACTIVE AQUATIC MUSEUM AT BAGH IBN QASIM CLIFTON KARACHI
INTERACTIVE AQUATIC MUSEUM AT BAGH IBN QASIM CLIFTON KARACHIKiranKandhro1
 
my goal is place in mnc's companies and got good salary
my goal is place in mnc's companies and got good salarymy goal is place in mnc's companies and got good salary
my goal is place in mnc's companies and got good salarymonoarul2004
 
Center Enamel is the leading fire water tanks manufacturer in China.docx
Center Enamel is the leading fire water tanks manufacturer in China.docxCenter Enamel is the leading fire water tanks manufacturer in China.docx
Center Enamel is the leading fire water tanks manufacturer in China.docxsjzzztc
 
chap. 3. lipid deterioration oil and fat processign
chap. 3. lipid deterioration oil and fat processignchap. 3. lipid deterioration oil and fat processign
chap. 3. lipid deterioration oil and fat processignteddymebratie
 
nervous system ppt pptx anatomy system of nerves
nervous system ppt pptx anatomy system of nervesnervous system ppt pptx anatomy system of nerves
nervous system ppt pptx anatomy system of nervesPhebeLois1
 
MedTech R&D - Tamer Emara - resume @2024
MedTech R&D - Tamer Emara - resume @2024MedTech R&D - Tamer Emara - resume @2024
MedTech R&D - Tamer Emara - resume @2024Tamer Emara
 
SR Globals Profile - Building Vision, Exceeding Expectations.
SR Globals Profile -  Building Vision, Exceeding Expectations.SR Globals Profile -  Building Vision, Exceeding Expectations.
SR Globals Profile - Building Vision, Exceeding Expectations.srglobalsenterprises
 
DOC-20240215-WA0000..pdf class 11 check waves
DOC-20240215-WA0000..pdf class 11 check wavesDOC-20240215-WA0000..pdf class 11 check waves
DOC-20240215-WA0000..pdf class 11 check wavesshreyanshdubey7814
 
ROBOT PERCEPTION FOR AGRICULTURE AND GOOD PRODUCTION1.1.pdf
ROBOT PERCEPTION FOR AGRICULTURE AND GOOD PRODUCTION1.1.pdfROBOT PERCEPTION FOR AGRICULTURE AND GOOD PRODUCTION1.1.pdf
ROBOT PERCEPTION FOR AGRICULTURE AND GOOD PRODUCTION1.1.pdfRudraPratapSingh871925
 

Recently uploaded (20)

MAXIMUM POWER POINT TRACKING ALGORITHMS APPLIED TO WIND-SOLAR HYBRID SYSTEM
MAXIMUM POWER POINT TRACKING ALGORITHMS APPLIED TO WIND-SOLAR HYBRID SYSTEMMAXIMUM POWER POINT TRACKING ALGORITHMS APPLIED TO WIND-SOLAR HYBRID SYSTEM
MAXIMUM POWER POINT TRACKING ALGORITHMS APPLIED TO WIND-SOLAR HYBRID SYSTEM
 
Nexus - Final Day 12th February 2024.pptx
Nexus - Final Day 12th February 2024.pptxNexus - Final Day 12th February 2024.pptx
Nexus - Final Day 12th February 2024.pptx
 
Microstrip Bandpass Filter Design using EDA Tolol such as keysight ADS and An...
Microstrip Bandpass Filter Design using EDA Tolol such as keysight ADS and An...Microstrip Bandpass Filter Design using EDA Tolol such as keysight ADS and An...
Microstrip Bandpass Filter Design using EDA Tolol such as keysight ADS and An...
 
Introduction about Technology roadmap for Industry 4.0
Introduction about Technology roadmap for Industry 4.0Introduction about Technology roadmap for Industry 4.0
Introduction about Technology roadmap for Industry 4.0
 
Gate-Level Simulation Methodology Improving Gate-Level Simulation Performance
Gate-Level Simulation Methodology Improving Gate-Level Simulation PerformanceGate-Level Simulation Methodology Improving Gate-Level Simulation Performance
Gate-Level Simulation Methodology Improving Gate-Level Simulation Performance
 
Introduction to the telecom tower industry
Introduction to the telecom tower industryIntroduction to the telecom tower industry
Introduction to the telecom tower industry
 
GDSC solution challenge Android ppt.pptx
GDSC solution challenge Android ppt.pptxGDSC solution challenge Android ppt.pptx
GDSC solution challenge Android ppt.pptx
 
CCNA: Routing and Switching Fundamentals
CCNA: Routing and Switching FundamentalsCCNA: Routing and Switching Fundamentals
CCNA: Routing and Switching Fundamentals
 
Get start with Machine Learning and Vertexai
Get start with Machine Learning and VertexaiGet start with Machine Learning and Vertexai
Get start with Machine Learning and Vertexai
 
Architectural Preservation - Heritage, focused on Saudi Arabia
Architectural Preservation - Heritage, focused on Saudi ArabiaArchitectural Preservation - Heritage, focused on Saudi Arabia
Architectural Preservation - Heritage, focused on Saudi Arabia
 
21 SCHEME_21EC53_VTU_MODULE-4_COMPUTER COMMUNCATION NETWORK.pdf
21 SCHEME_21EC53_VTU_MODULE-4_COMPUTER COMMUNCATION NETWORK.pdf21 SCHEME_21EC53_VTU_MODULE-4_COMPUTER COMMUNCATION NETWORK.pdf
21 SCHEME_21EC53_VTU_MODULE-4_COMPUTER COMMUNCATION NETWORK.pdf
 
INTERACTIVE AQUATIC MUSEUM AT BAGH IBN QASIM CLIFTON KARACHI
INTERACTIVE AQUATIC MUSEUM AT BAGH IBN QASIM CLIFTON KARACHIINTERACTIVE AQUATIC MUSEUM AT BAGH IBN QASIM CLIFTON KARACHI
INTERACTIVE AQUATIC MUSEUM AT BAGH IBN QASIM CLIFTON KARACHI
 
my goal is place in mnc's companies and got good salary
my goal is place in mnc's companies and got good salarymy goal is place in mnc's companies and got good salary
my goal is place in mnc's companies and got good salary
 
Center Enamel is the leading fire water tanks manufacturer in China.docx
Center Enamel is the leading fire water tanks manufacturer in China.docxCenter Enamel is the leading fire water tanks manufacturer in China.docx
Center Enamel is the leading fire water tanks manufacturer in China.docx
 
chap. 3. lipid deterioration oil and fat processign
chap. 3. lipid deterioration oil and fat processignchap. 3. lipid deterioration oil and fat processign
chap. 3. lipid deterioration oil and fat processign
 
nervous system ppt pptx anatomy system of nerves
nervous system ppt pptx anatomy system of nervesnervous system ppt pptx anatomy system of nerves
nervous system ppt pptx anatomy system of nerves
 
MedTech R&D - Tamer Emara - resume @2024
MedTech R&D - Tamer Emara - resume @2024MedTech R&D - Tamer Emara - resume @2024
MedTech R&D - Tamer Emara - resume @2024
 
SR Globals Profile - Building Vision, Exceeding Expectations.
SR Globals Profile -  Building Vision, Exceeding Expectations.SR Globals Profile -  Building Vision, Exceeding Expectations.
SR Globals Profile - Building Vision, Exceeding Expectations.
 
DOC-20240215-WA0000..pdf class 11 check waves
DOC-20240215-WA0000..pdf class 11 check wavesDOC-20240215-WA0000..pdf class 11 check waves
DOC-20240215-WA0000..pdf class 11 check waves
 
ROBOT PERCEPTION FOR AGRICULTURE AND GOOD PRODUCTION1.1.pdf
ROBOT PERCEPTION FOR AGRICULTURE AND GOOD PRODUCTION1.1.pdfROBOT PERCEPTION FOR AGRICULTURE AND GOOD PRODUCTION1.1.pdf
ROBOT PERCEPTION FOR AGRICULTURE AND GOOD PRODUCTION1.1.pdf
 

Pointers.pptx

  • 1. UNIT III POINTERS AND STRING P.ANANTHI, Assistant Professor, Kongu Engineering College 1
  • 2. TOPICS: POINTER • MEMORY ACCESS AND POINTERS • POINTER BASICS • DECLARING • INITIALIZING • DEREFERENCING A POINTER • PARAMETER PASSING MECHANISM • OPERATIONS ON POINTER 2 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 3. MEMORY ACCESS Memory access refers to the ability to read from or write to specific locations in the computer's memory. The memory in a computer is organized into a sequence of bytes, each with a unique address. 3 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 4. MEMORY ACCESS • Every variable is a memory location and every memory location has its address defined which can be accessed through ampersand (&) operator, which denotes an address in memory 4 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 5. 5 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 6. Address of a: 0x7ffd7218d764 Address of b: 0x7ffd7218d760 Address of c: 0x7ffd7218d75f #include <stdio.h> int main() { int a = 10; double b = 20.5; char c = 'A'; printf("Address of a: %pn", &a); printf("Address of b: %pn", &b); printf("Address of c: %pn", &c); return 0; } 6 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 7. Variables • Variables in C are named storage locations that hold values of a specific data type. • When you declare a variable, the compiler allocates memory to store its value, and you can access that memory using the variable's name. int age = 25; // Declaring an integer variable 'age' and assigning it a value 7 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 8. Pointer • Pointers are variables that store memory addresses. They allow direct access to the memory location they point to. • Pointers are declared with a specific data type, indicating the type of data stored at the memory location. • Example, int a=10; • int *ptr; // Declaration of an integer pointer 10 10 a Location Value Name of the variable 80F Address of the location 8 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 9. Pointer • Value 10 can be accessed by using the variable name or the address 80F • Memory address are simply numbers can be assigned to some other variable. • The variable that holds the memory address is called pointer variable 10 10 a 80F 10 80F ptr 82C Pointer name Address of the pointer Address of a 9 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 10. Pointer • Value 10 can be accessed by using the variable name or the address 80F • Memory address are simply numbers can be assigned to some other variable. • The variable that holds the memory address is called pointer variable 10 10 a 80F 10 80F ptr 82C Pointer name Address of the pointer Address of a 10 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 11. Address of operator • The address-of operator (&) is used to get the memory address of a variable. int age = 25; int *ptr = &age; // Assigning the address of 'age' to the pointer 'ptr' 11 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 12. Dereferencing operator • The dereference operator (*) is used to access the value stored at a memory address pointed to by a pointer. int age = 25; int *ptr = &age; printf("Value at the memory address pointed by ptr: %dn", *ptr); 12 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 13. • Sample Program 1 • Pointer declaration and initialization • Sample Program 2 • Pointer Basics 13 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 14. Void pointer • Pointer that is not associated with any data type. • It can point to objects of any type but doesn't provide information about the type of data it points to. • Void pointer sample Program • Syntax: • Void *pointer name; 14 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 15. Null pointer • A null pointer in C is a pointer that does not point to any memory location. • It is a special constant value (often represented as 0 or NULL) that is assigned to a pointer variable to indicate that it does not currently point to a valid memory address. • Null pointer sample program • Syntax: • Int *p=NULL; 15 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 16. Parameter passing Pass by value Pass by reference 16 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 17. Pass by value • The value of the argument is passed to the function. • Changes made to the parameter inside the function do not affect the original value. • Sample Pgm 17 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 18. #include <stdio.h> void incrementByValue(int x) { x++; printf("Inside function: x = %dn", x); } int main() { int num = 5; printf("Before function call: num = %dn", num); incrementByValue(num); printf("After function call: num = %dn", num); return 0; } 18 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 19. Pass by reference • The address of the argument is passed to the function using a pointer. • Changes made to the parameter inside the function affect the original value. • Sample Pgm 19 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 20. #include <stdio.h> void incrementByReference(int *x) { (*x)++; printf("Inside function: *x = %dn", *x); } int main() { int num = 5; printf("Before function call: num = %dn", num); incrementByReference(&num); printf("After function call: num = %dn", num); return 0; } 20 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 21. Keep in mind • The datatype of a pointer is different from the type of the variable it points to • The size of a pointer is generally fixed and does not depend on the size of the object it points to • A pointer can be assigned to 0 • An uninitialized or null pointer must not be dereferenced • Adding 1 to a pointer variable doesn't necessarily add 1 to the value stored by the pointer • A pointer can snap its current link and points to another variable 21 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 22. Passing array to function using pointer • A single array element or a entire array can be passed to a function. 22 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 23. #include <stdio.h> int main() { // Declare an array int numbers[] = {10, 20, 30, 40, 50}; // Declare a pointer and point it to the first element of the array int *ptr = numbers; // Accessing array elements using pointer printf("First element: %dn", *ptr); // Output: 10 printf("Second element: %dn", *(ptr + 1)); // Output: 20 printf("Third element: %dn", *(ptr + 2)); // Output: 30 // You can also use array subscript notation with pointers printf("Fourth element: %dn", ptr[3]); // Output: 40 return 0; } 23 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 24. • Likewise, one dimensional and multi dimensional array can be passed to a function as argument One Dimension: • While passing arrays to the argument, the name of the array is passed as an argument • 1 D ARRAY USING POINTER 24 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 25. • TWO DIMENSION • To pass a 2D array to a function as an argument, starting address of memory area reserved is passed as in 1D array • 2D array to a function 25 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 26. Function pointer • Function pointers in C allow you to store and call functions through pointers. They provide a way to create more flexible and dynamic code. • Syntax: • returnType (*pointerName)(parameterTypes); • Function pointer 26 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 27. Pointer operations • Pointer operations in C involve manipulating memory addresses using pointers. • Dereferencing • Pointer Arithmetic • Array Access with Pointers • Pointer Comparison • Pointer to Pointer (Double Pointer) • Dynamic Memory Allocation 27 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 28. Dereferencing • Accessing the value stored at the memory location pointed to by a pointer int x = 10; int *ptr = &x; printf("Value at the memory location: %dn", *ptr); 28 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 29. Pointer Arithmetic • Incrementing or decrementing a pointer to move to the next or previous memory location. int arr[] = {1, 2, 3, 4, 5}; int *ptr = arr; // Points to the first element of the array printf("First element: %dn", *ptr); ptr++; // Move to the next element printf("Second element: %dn", *ptr); 29 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 30. Array Access with Pointers • Using pointers to iterate through an array. int arr[] = {1, 2, 3, 4, 5}; int *ptr = arr; for (int i = 0; i < 5; ++i) { printf("%d ", *(ptr + i)); } 30 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 31. Pointer Comparison • Comparing two pointers to check their relative positions in memory. int arr[] = {1, 2, 3, 4, 5}; int *ptr1 = arr; int *ptr2 = &arr[2]; if (ptr1 < ptr2) { printf("ptr1 comes before ptr2 in memory.n"); } 31 P.ANANTHI, Assistant Professor, Kongu Engineering College
  • 32. Pointer to Pointer (Double Pointer) • Using pointers to point to other pointers int x = 10; int *ptr1 = &x; int **ptr2 = &ptr1; printf("Value at the memory location pointed by ptr2: %dn", **ptr2); 32 P.ANANTHI, Assistant Professor, Kongu Engineering College