SlideShare a Scribd company logo
1 of 19
Chapter 5
POINTERs
POINTERS
What is a pointer ?
A pointer is nothing but a variable that contains
the address which is a location of the another variable in
memory.
Benefits of using the pointer :
1.A pointer enables us to access a variable that is
defined outside the function.
2.Pointer are more efficient in handling data table.
3.Pointer reduces the length and complexity of the
program.
4.The use of pointer array to character string results in
saving of data storage space in memory.
Declaring and initializing pointers :
syntax :
data_type *pt_name ;
This tells the compiler three things about the
variable pt_name.
1. * tells the compiler that the variable
pt_name is a pointer variable.
2. pt_name needs a memory location.
3. pt_name points to a variable of type
data_type.
•Ex: int quantity=179 ;
int *p;
p=&quantity;
quantity variable
179 value
5000 Address
p=&quantity;
Note : you can know the address of variable
using %u format specification.
You can’t assign an absolute address to a
pointer variable directly.
Ex: p = 5000; It is not possible
Accessing variables using pointers.
void main()
{ int x,y, *ptr;
x=10;
ptr=&x;
y = *ptr;
printf(“Value of x is %dnn”,x);
printf(“%d is stored at address %un”, x, &x);
printf(“%d is stored at address %un”, *&x, &x);
printf(“%d is stored at address %un”, *ptr, &x);
printf(“%d is stored at address %un”, y, &*ptr);
printf(“%d is stored at address %un”,ptr,&ptr);
printf(“%d is stored at address %un”, y, &y);
*ptr=25;
printf(“n Now x = %dn”,x)
}
Output:
Value of X is 10
10 is stored at address
4104
10 is stored at address
4104
10 is stored at address
4104
10 is stored at address
4104
4104 is stored at address
4106
10 is stored at address
4108
Now x = 25
•Pointers expression :
void main()
{
int a,b,*p1,*p2, x, y, z ;
a =12; b = 4;
p1=&a; p2 = &b;
x = *p1 * *p2 – 6;
printf(“Address of a = %un”,p1);
printf(“Address of b=%un”,p2);
printf(“n”);
printf(“a=%d, b=%dn”,a , b);
printf(“x=%d n”,x);
*p2 = *p2 + 3 ;
*p1 = *p2 – 5 ;
y = *p1 + *p2;
z= *p1 * *p2 – 6 ;
printf(n a = %d , b = %d “, a ,b);
printf(“z = %dn”, z);
}
Out put :-
Address of a = 4020
Address of b =4016
a = 12 b=4
x=42 y=9
a=2 b=7 z=8
Pointer increments and scale factor :
 p1++ will cause the pointer p1 points to the
next value of its type.
 Ex :- if p1 is an integer pointer with the initial
value say 2800,then after the operations
p1=p1+1,the value of p1 will be 2802,not 2801
 when we increment pointer its value is
increased by the length of the data type that it
points to. This length is called scale factor.
Pointer and Arrays :-
When an array is declared, the compiler allocates a
base
Address and sufficient amount of storage to contains
all
The elements of the array in memory location.
Base address is the location of the first element of the
array
int x[5] = {1,2,3,4,5}
Element x[0] x[1] x[2] x[3] x[4]
Value 1 2 3 4 5
Address 1000 1002 1004 1006 1008
So, base address is,
x = &x[0] = 1000
If we declare p as integer pointer, then we can
make the pointer p to point to the array x by the
following statements.
p = x or p = &x[0]
Now we can access every element of x using
p++ to move from one element to another.
p = &x[0] (=1000)
p+1 = &x[1] (=1002)
p+2 = &x[2] (=1004)
p+3 = &x[3] (=1006)
p+4 = &x[4] (=1008)
/*Program of sum n array elements*/
#include<stdio.h>
main()
{
int x[10],i,*p,n,sum=0;
printf("enter the N:");
scanf("%d",&n);
printf("enter the the data:n");
for(i=0;i<n;i++)
scanf("%d",&x[i]);
p = x;
/*Program of sum n array elements*/
for(i=0;i<n;i++)
{
printf("n%d",*p);
sum=sum + *p;
p++;
}
printf("nsum=%d",sum);
}
OUTPUT
Enter the N: 5
Enter the data :
1 2 3 4 5
Sum=15
2,0 2,3
P
P +
1
0 1 2 3 4
0
1
2
3
*(p+2)
p+2
p = pointer to first row
p+i = pointer to ith row
*(p+i) = pointer to first element in the ith row
*(p+i)+j = pointer to jth elements
*(*(p+i)+j)=value stored in the cell(i,j)
*(p+2) + 3
Array using pointer
notation
/* Program of sum of n array elements*/
#include<stdio.h>
void main()
{
int x[10][10],i,j,n,sum=0;
clrscr();
printf("enter the N:");
scanf("%d",&n);
printf("enter the the data:n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("n%d",*(*(x+i)+j));
sum=sum + *(*(x+i)+j);
}
}
printf("nsum=%d",sum);
getch();
}
Pointer and character string :-
 In c, a constant character string always
represents a pointer to that string. And therefore
you can directly write char *name;
name = “DELHI”
 But remember that this type of
assignment does not apply on character
arrays.
char name[20];
name = “DELHI”
char *name[3] = {“New
Zealand”,“Australia”,“India”}
Where name to be an array of three pointers
to a character, each pointer points to a particular
name.
name[0]=“New Zealand”,
name[1]=“Australia”,
name[2]=“India”
This declaration allocates only 28 bytes.
The character arrays with the rows of varying
length are called ragged array.
/****PROGRAMM FOR STRING PRINT USING
POINTER ****/
#include<stdio.h>
void main()
{
char name[10],*ptr;
clrscr();
printf("enter the name => ");
scanf("%s",name);
ptr=name;
printf("n");
while(*ptr != '0')
{
printf("%c",*ptr);
ptr++;
}
getch();
}
Note : if we want to directly assign string
constant then we have to make name as character
pointer.
char *name ; name = “Delhi”

More Related Content

Similar to Chapter5.pptx

Similar to Chapter5.pptx (20)

Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
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
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
CSE240 Pointers
CSE240 PointersCSE240 Pointers
CSE240 Pointers
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers
PointersPointers
Pointers
 
9.C Programming
9.C Programming9.C Programming
9.C Programming
 
Engineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptxEngineering Computers L32-L33-Pointers.pptx
Engineering Computers L32-L33-Pointers.pptx
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
Pointers
PointersPointers
Pointers
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
C programming
C programmingC programming
C programming
 
pointers 1
pointers 1pointers 1
pointers 1
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Ch 7-pointers
Ch 7-pointersCh 7-pointers
Ch 7-pointers
 

Recently uploaded

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
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
 
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
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
_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
 
“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
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
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
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
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
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
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
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
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 🔝✔️✔️
 
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
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
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🔝
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
_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
 
“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...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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
 
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
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
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...
 

Chapter5.pptx

  • 2. POINTERS What is a pointer ? A pointer is nothing but a variable that contains the address which is a location of the another variable in memory. Benefits of using the pointer : 1.A pointer enables us to access a variable that is defined outside the function. 2.Pointer are more efficient in handling data table. 3.Pointer reduces the length and complexity of the program. 4.The use of pointer array to character string results in saving of data storage space in memory.
  • 3. Declaring and initializing pointers : syntax : data_type *pt_name ; This tells the compiler three things about the variable pt_name. 1. * tells the compiler that the variable pt_name is a pointer variable. 2. pt_name needs a memory location. 3. pt_name points to a variable of type data_type.
  • 4. •Ex: int quantity=179 ; int *p; p=&quantity; quantity variable 179 value 5000 Address p=&quantity; Note : you can know the address of variable using %u format specification. You can’t assign an absolute address to a pointer variable directly. Ex: p = 5000; It is not possible
  • 5. Accessing variables using pointers. void main() { int x,y, *ptr; x=10; ptr=&x; y = *ptr; printf(“Value of x is %dnn”,x); printf(“%d is stored at address %un”, x, &x); printf(“%d is stored at address %un”, *&x, &x); printf(“%d is stored at address %un”, *ptr, &x); printf(“%d is stored at address %un”, y, &*ptr); printf(“%d is stored at address %un”,ptr,&ptr); printf(“%d is stored at address %un”, y, &y); *ptr=25; printf(“n Now x = %dn”,x) } Output: Value of X is 10 10 is stored at address 4104 10 is stored at address 4104 10 is stored at address 4104 10 is stored at address 4104 4104 is stored at address 4106 10 is stored at address 4108 Now x = 25
  • 6. •Pointers expression : void main() { int a,b,*p1,*p2, x, y, z ; a =12; b = 4; p1=&a; p2 = &b; x = *p1 * *p2 – 6; printf(“Address of a = %un”,p1); printf(“Address of b=%un”,p2); printf(“n”); printf(“a=%d, b=%dn”,a , b); printf(“x=%d n”,x);
  • 7. *p2 = *p2 + 3 ; *p1 = *p2 – 5 ; y = *p1 + *p2; z= *p1 * *p2 – 6 ; printf(n a = %d , b = %d “, a ,b); printf(“z = %dn”, z); } Out put :- Address of a = 4020 Address of b =4016 a = 12 b=4 x=42 y=9 a=2 b=7 z=8
  • 8. Pointer increments and scale factor :  p1++ will cause the pointer p1 points to the next value of its type.  Ex :- if p1 is an integer pointer with the initial value say 2800,then after the operations p1=p1+1,the value of p1 will be 2802,not 2801  when we increment pointer its value is increased by the length of the data type that it points to. This length is called scale factor.
  • 9. Pointer and Arrays :- When an array is declared, the compiler allocates a base Address and sufficient amount of storage to contains all The elements of the array in memory location. Base address is the location of the first element of the array int x[5] = {1,2,3,4,5} Element x[0] x[1] x[2] x[3] x[4] Value 1 2 3 4 5 Address 1000 1002 1004 1006 1008 So, base address is, x = &x[0] = 1000
  • 10. If we declare p as integer pointer, then we can make the pointer p to point to the array x by the following statements. p = x or p = &x[0] Now we can access every element of x using p++ to move from one element to another. p = &x[0] (=1000) p+1 = &x[1] (=1002) p+2 = &x[2] (=1004) p+3 = &x[3] (=1006) p+4 = &x[4] (=1008)
  • 11. /*Program of sum n array elements*/ #include<stdio.h> main() { int x[10],i,*p,n,sum=0; printf("enter the N:"); scanf("%d",&n); printf("enter the the data:n"); for(i=0;i<n;i++) scanf("%d",&x[i]); p = x;
  • 12. /*Program of sum n array elements*/ for(i=0;i<n;i++) { printf("n%d",*p); sum=sum + *p; p++; } printf("nsum=%d",sum); } OUTPUT Enter the N: 5 Enter the data : 1 2 3 4 5 Sum=15
  • 13. 2,0 2,3 P P + 1 0 1 2 3 4 0 1 2 3 *(p+2) p+2 p = pointer to first row p+i = pointer to ith row *(p+i) = pointer to first element in the ith row *(p+i)+j = pointer to jth elements *(*(p+i)+j)=value stored in the cell(i,j) *(p+2) + 3 Array using pointer notation
  • 14. /* Program of sum of n array elements*/ #include<stdio.h> void main() { int x[10][10],i,j,n,sum=0; clrscr(); printf("enter the N:"); scanf("%d",&n); printf("enter the the data:n"); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&x[i][j]);
  • 16. Pointer and character string :-  In c, a constant character string always represents a pointer to that string. And therefore you can directly write char *name; name = “DELHI”  But remember that this type of assignment does not apply on character arrays. char name[20]; name = “DELHI”
  • 17. char *name[3] = {“New Zealand”,“Australia”,“India”} Where name to be an array of three pointers to a character, each pointer points to a particular name. name[0]=“New Zealand”, name[1]=“Australia”, name[2]=“India” This declaration allocates only 28 bytes. The character arrays with the rows of varying length are called ragged array.
  • 18. /****PROGRAMM FOR STRING PRINT USING POINTER ****/ #include<stdio.h> void main() { char name[10],*ptr; clrscr(); printf("enter the name => "); scanf("%s",name); ptr=name; printf("n");
  • 19. while(*ptr != '0') { printf("%c",*ptr); ptr++; } getch(); } Note : if we want to directly assign string constant then we have to make name as character pointer. char *name ; name = “Delhi”