SlideShare a Scribd company logo
1 of 18
Pointers
Created By:
Name: Abhimanyu Mehta
V.V.P Engineering College, Rajkot
Pointers
Introduction
• A Pointer is a derived data type in C.
• Pointers contains memory addresses as their
values.
• Pointer reduces the length and complexity of
the program.
• It allows working with dynamically allocated
memory.
• Definition:
Pointer variable: Pointer variable is a variable
that contains an address, which is a location of
another variable in memory.
Declaring pointer variables
• The declaration of the pointer variable takes
the following form:
data_type *pt_name;
Initialization of pointer variable
• The process of assigning the address of a
variable to a pointer variable is known as
initialization.
• Initialization:
int *p=#
Example
• W.a.p to illustrate the use of indirection operator ‘*’ to access the value pointed to
by a pointer.
#include<stdio.h>
void main()
{
int x,y;
int *ptr;
x=10;
ptr=&x;
y=*ptr;
clrscr();
printf(“n%d”,x);
printf(“n %d is stored at address %u”,x,&x);
printf(“n %d is stored at address %u”,*&x,&x);
printf(“n %d is stored at address %u”,*ptr,ptr);
printf(“n %u is stored at address %u”,ptr,&ptr);
printf(“n %d is stored at address %u”,y,&y);
*ptr=30;
printf(“n now x=%d”,x);
getch();
}
Output:
Value of x =10
10 is stored at address 65524
10 is stored at address 65524
10 is stored at address 65524
65524 is stored at address 65520
10 is stored at address 65522
Now x =30
Pointer to pointer
• A variable that is pointer to pointer must be
declared using additional indirection operator
symbols in front of the name.
int **p2;
Example
w.a.p to demonstrate the pointer to pointer.
#include<stdio.h>
void main()
{
char c=‘z’,*cp,**pcp;
float f=10.2;*fp,**pfp;
int i=987,*ip,**pip;
cp=&c;
pcp=&cp;
fp=&f;
pfp=&fp;
ip=&i;
pip=&ip;
clrscr();
printf(“n”);
printf(“n c=%c, cp=%u, pcp=%u”, c,&cp,&pcp);
printf(“n i=%d, ip=%u, pip=%u”, i,&ip,&pip);
printf(“n f=%f, fp=%u, pfp=%u”, f,&fp,&pfp);
getch();
}
Output:
c=z,cp=65522,pcp=65520
i=987,ip=65508,pip=65506
f=10.200000,fp=65514,pfp=65512
Pointers and Arrays
w.a.p to find out maximum number from 1-D array using pointer.
#include<stdio.h>
void main()
{
int a[5],i;
int *p,max=0;
clrscr();
p=a;
for(i=0;i<5;i++)
{
printf(“ a[%d]”,i);
scanf(“%d”,p);
p++;
}
{
max=*p;
}
p++;
}
printf(“n max=%d”,max);
getch();
}
Output:
Enter the value for a[0]= 12
Enter the value for a[1]= 23
Enter the value for a[2]= 3
Enter the value for a[3]= 40
Enter the value for a[4]= 51
Max=51
Arrays of pointers
• Arrays of pointers is a collection of pointers of
same data type.
• Syntax:
data type *name[size];
Example
Demonstrate the use of pointer in 2-D.
#include<stdio.h>
void main()
{
int m[2][2];
int i,j;
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“nEnter value for row = %d,column= %d:-”,i,j);
scanf(“%d”,m[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“m[%d][%d]=%d”,i,j,*(*(m+i)+j));
}
printf(“n”);
}
getch();
}
Output:
Enter the value for row=0, column=0:-1
Enter the value for row=0, column=1:-2
Enter the value for row=1, column=0:-3
Enter the value for row=1, column=1:-4
m[0][0]=1 m[0][1]=2
m[1][0]=3 m[1][1]=4
THANK YOU

More Related Content

What's hot

Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Dharma Kshetri
 
Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Digvijaysinh Gohil
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingMeghaj Mallick
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 

What's hot (20)

C Structure and Union in C
C Structure and Union in CC Structure and Union in C
C Structure and Union in C
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
String searching
String searchingString searching
String searching
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
Pointer to function 1
Pointer to function 1Pointer to function 1
Pointer to function 1
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Functions (Computer programming and utilization)
Functions (Computer programming and utilization)Functions (Computer programming and utilization)
Functions (Computer programming and utilization)
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 

Viewers also liked

Vf streaming
Vf streamingVf streaming
Vf streamingwawacity3
 
Laura martinez. 1
Laura martinez. 1Laura martinez. 1
Laura martinez. 1anitacc1
 
01. om shanti media nov - i - 2012
01. om shanti media   nov - i - 201201. om shanti media   nov - i - 2012
01. om shanti media nov - i - 2012Kamlesh Shete
 
Business ppt-template-004
Business ppt-template-004Business ppt-template-004
Business ppt-template-004mygezegen
 
Demencia frontotemporal
Demencia frontotemporalDemencia frontotemporal
Demencia frontotemporalconsuelo cerda
 
Kumpulan soal biologi METABOLISME besrta kunci jawaban
Kumpulan soal biologi METABOLISME besrta kunci jawabanKumpulan soal biologi METABOLISME besrta kunci jawaban
Kumpulan soal biologi METABOLISME besrta kunci jawabanSyarifah Thahira Anasya
 

Viewers also liked (13)

Vf streaming
Vf streamingVf streaming
Vf streaming
 
Prevencion trabajo oficina parte 2.
Prevencion trabajo oficina parte 2.Prevencion trabajo oficina parte 2.
Prevencion trabajo oficina parte 2.
 
Presentación1
Presentación1Presentación1
Presentación1
 
Bitacora
BitacoraBitacora
Bitacora
 
Laura martinez. 1
Laura martinez. 1Laura martinez. 1
Laura martinez. 1
 
01. om shanti media nov - i - 2012
01. om shanti media   nov - i - 201201. om shanti media   nov - i - 2012
01. om shanti media nov - i - 2012
 
Prevencion trabajo oficina parte 3.
Prevencion trabajo oficina parte 3.Prevencion trabajo oficina parte 3.
Prevencion trabajo oficina parte 3.
 
Film 2014
Film 2014Film 2014
Film 2014
 
Prevención trabajo oficina parte 4.
Prevención trabajo oficina parte 4.Prevención trabajo oficina parte 4.
Prevención trabajo oficina parte 4.
 
Business ppt-template-004
Business ppt-template-004Business ppt-template-004
Business ppt-template-004
 
Presentación1
Presentación1Presentación1
Presentación1
 
Demencia frontotemporal
Demencia frontotemporalDemencia frontotemporal
Demencia frontotemporal
 
Kumpulan soal biologi METABOLISME besrta kunci jawaban
Kumpulan soal biologi METABOLISME besrta kunci jawabanKumpulan soal biologi METABOLISME besrta kunci jawaban
Kumpulan soal biologi METABOLISME besrta kunci jawaban
 

Similar to Pointers

Similar to Pointers (20)

4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 
Pointers.pdf
Pointers.pdfPointers.pdf
Pointers.pdf
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers
PointersPointers
Pointers
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Pointers
PointersPointers
Pointers
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Pointers.pptx
Pointers.pptxPointers.pptx
Pointers.pptx
 

Recently uploaded

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
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
 
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
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
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
 
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
 
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
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
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
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
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
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 

Recently uploaded (20)

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
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
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar 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🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
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
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
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...
 
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
 
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 🔝✔️✔️
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
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
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
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
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 

Pointers

  • 1. Pointers Created By: Name: Abhimanyu Mehta V.V.P Engineering College, Rajkot
  • 3. Introduction • A Pointer is a derived data type in C. • Pointers contains memory addresses as their values. • Pointer reduces the length and complexity of the program. • It allows working with dynamically allocated memory.
  • 4. • Definition: Pointer variable: Pointer variable is a variable that contains an address, which is a location of another variable in memory.
  • 5. Declaring pointer variables • The declaration of the pointer variable takes the following form: data_type *pt_name;
  • 6. Initialization of pointer variable • The process of assigning the address of a variable to a pointer variable is known as initialization. • Initialization: int *p=&num;
  • 7. Example • W.a.p to illustrate the use of indirection operator ‘*’ to access the value pointed to by a pointer. #include<stdio.h> void main() { int x,y; int *ptr; x=10; ptr=&x; y=*ptr; clrscr(); printf(“n%d”,x); printf(“n %d is stored at address %u”,x,&x); printf(“n %d is stored at address %u”,*&x,&x); printf(“n %d is stored at address %u”,*ptr,ptr);
  • 8. printf(“n %u is stored at address %u”,ptr,&ptr); printf(“n %d is stored at address %u”,y,&y); *ptr=30; printf(“n now x=%d”,x); getch(); } Output: Value of x =10 10 is stored at address 65524 10 is stored at address 65524 10 is stored at address 65524 65524 is stored at address 65520 10 is stored at address 65522 Now x =30
  • 9. Pointer to pointer • A variable that is pointer to pointer must be declared using additional indirection operator symbols in front of the name. int **p2;
  • 10. Example w.a.p to demonstrate the pointer to pointer. #include<stdio.h> void main() { char c=‘z’,*cp,**pcp; float f=10.2;*fp,**pfp; int i=987,*ip,**pip; cp=&c; pcp=&cp; fp=&f; pfp=&fp; ip=&i; pip=&ip; clrscr(); printf(“n”); printf(“n c=%c, cp=%u, pcp=%u”, c,&cp,&pcp);
  • 11. printf(“n i=%d, ip=%u, pip=%u”, i,&ip,&pip); printf(“n f=%f, fp=%u, pfp=%u”, f,&fp,&pfp); getch(); } Output: c=z,cp=65522,pcp=65520 i=987,ip=65508,pip=65506 f=10.200000,fp=65514,pfp=65512
  • 12. Pointers and Arrays w.a.p to find out maximum number from 1-D array using pointer. #include<stdio.h> void main() { int a[5],i; int *p,max=0; clrscr(); p=a; for(i=0;i<5;i++) { printf(“ a[%d]”,i); scanf(“%d”,p); p++; }
  • 13. { max=*p; } p++; } printf(“n max=%d”,max); getch(); } Output: Enter the value for a[0]= 12 Enter the value for a[1]= 23 Enter the value for a[2]= 3 Enter the value for a[3]= 40 Enter the value for a[4]= 51 Max=51
  • 14. Arrays of pointers • Arrays of pointers is a collection of pointers of same data type. • Syntax: data type *name[size];
  • 15. Example Demonstrate the use of pointer in 2-D. #include<stdio.h> void main() { int m[2][2]; int i,j; clrscr(); for(i=0;i<2;i++) { for(j=0;j<2;j++) { printf(“nEnter value for row = %d,column= %d:-”,i,j);
  • 17. Output: Enter the value for row=0, column=0:-1 Enter the value for row=0, column=1:-2 Enter the value for row=1, column=0:-3 Enter the value for row=1, column=1:-4 m[0][0]=1 m[0][1]=2 m[1][0]=3 m[1][1]=4