SlideShare a Scribd company logo
1 of 30
Download to read offline
PointersPointers
Pointer
• A pointer is a variable that represents the
location (rather than the value) of a data
item
• Indirect Variable
• pointers can be used to pass information back• pointers can be used to pass information back
and forth between a function and its reference
point
• pointers provide a way to return multiple data
items from a function via function arguments
Contd…
• pointers also permit references to other
functions to be specified as arguments to a
given function
• pointers are also closely associated with• pointers are also closely associated with
arrays and therefore provide an alternate
way to access individual array elements
• int *u
u is a pointer that store address
#include<stdio.h>
main( )
{
int u=5;
int *pu;
pu=&u;pu=&u;
printf("n u=%d &u=%x pu=%x
*pu=%d",u,&u,pu,*pu);
}
#include<stdio.h>
main()
{
int u=5;
int *pu;
printf("n u=%d &u=%x pu=%xprintf("n u=%d &u=%x pu=%x
*pu=%d",u,&u,pu,*pu);
}
*pu and pu give garbage value
#include<stdio.h>
main()
{
int u=5;
int *pu=6;
printf("n u=%d &u=%x pu=%x
*pu=%d",u,&u,pu,*pu);*pu=%d",u,&u,pu,*pu);
}
Non portable pointer conversion
u=5, &u=fff4, pu=6, *pu=27762
#include<stdio.h>
main()
{
int u=5;
int *pu;
pu=&u;
printf("n u=%d &u=%x pu=%x *pu=%d",u,&u,pu,*pu);
*pu=u+2;*pu=u+2;
printf("n u=%d &u=%x pu=%x *pu=%d",u,&u,pu,*pu);
u=*pu*5;
printf("n u=%d &u=%x pu=%x *pu=%d",u,&u,pu,*pu);
}
PASSING POINTERS TO A
FUNCTION
• passing pointer as an argument to the function allows
data items within the calling portion of the program to
be accessed by the function, altered within the function,
and then returned to the calling portion of the program
in altered formin altered form
• passing pointers as arguments is known as passing by
reference (or by address or by location),in contrast to
passing arguments by value
void passingbyvalue(int u,int v)
{
u=0, v=0;
printf("n Within passingbyvalue
function: u=%d v=%d",u,v);
return;
}
void passingbyref(int *pu,int *pv)
{
*pu=0, *pv=0;
printf("n Within passingbyref
main()
{
int u=5,v=6;
printf("nBefore calling function
passingbyvalue: u=%d v=%d",u,v);
passingbyvalue(u,v);
printf("nAfter calling function
passingbyvalue: u=%d v=%d",u,v);
printf("nnBefore calling function
passingbyref: u=%d v=%d",u,v);
printf("n Within passingbyref
function: *pu=%d
*pv=%d",*pu,*pv);
return;
}
passingbyref(&u,&v);
printf("nAfter calling function
passingbyref: u=%d v=%d",u,v);
}
#include<stdio.h>
#include<ctype.h>
void scan_line(char line[],int *pv,int
*pc,int *pd,int *pw,int *po);
main()
{
char line[80];
int
vowels=0,consonants=0,digits=0,whi
tespc=0,other=0;
printf("Enter a line of text below:n");
scanf("%[^n]",line);
scan_line(line,&vowels,&consonants,&d
igits,&whitespc,&other);
void scan_line(char line[],int *pv,int *pc,int
*pd,int *pw,int *po)
{
char c;
int count=0;
while ((c=toupper(line[count]))!='0'){
if(c=='A'|| c=='E'|| c=='I'||c=='O'||
c=='U')
++*pv;
else if(c>='A' && c<='Z')
++*pc;
else if(c>='0' && c<='9')
++*pd;
else if(c ==' ' || c == 't')
++*pw;
elseigits,&whitespc,&other);
printf("nNo. of vowels:%d",vowels);
printf("nNo. of
consonants:%d",consonants);
printf("nNo. of digits:%d",digits);
printf("nNo. of whitespace
characters:%d",whitespc);
printf("nNo. of other
characters:%d",other);
}
++*po;
++count;
}
return;
}
POINTERS AND ONE-DIMENSIONAL ARRAYS
#include<stdio.h>
char x[ ] = "This string is declared
externallyn";
main()
{
static char y[ ] = "This string is
declared within main";
#include<stdio.h>
char *x= "This string is declared
externallyn";
main()
{
char *y = "This string is declared
within main";
printf("%s",x);
printf("%s",y);
}declared within main";
printf("%s",x);
printf("%s",y);
}
}
Contd…
• an array name is a pointer to the first element
in the array
• x is a one dimensional array, then the address
of the first array element can be expressed asof the first array element can be expressed as
either &x[0] or simply as x
#include<stdio.h>
main()
{
int x[5]={1,2,3,4,5},i;
for(i=0;i<5;++i){
printf("ni=%d x[%d]=%d *(x+%d)=%d",i,i,x[i],i,*(x+i));printf("ni=%d x[%d]=%d *(x+%d)=%d",i,i,x[i],i,*(x+i));
printf(" &x[%d]=%x x+%d=%x",i,&x[i],i,(x+i));
}
}
DYNAMIC MEMORY
ALLOCATION
• an array name is actually a pointer to the first
element within the array, it should be possible
to define the array as a pointer variable rather
than as a conventional array
• a conventional array definition results in a
fixed block of memory being reserved at the
beginning of program execution, whereas this
does not occur if the array is represented in
terms of a pointer variable
Contd…
• the use of a pointer variable to represent an array requires
some type of initial memory assignment before the array
elements are processed
• known as dynamic memory allocation
• the malloc library function is used for this purpose
• stdlib.h or malloc.h• stdlib.h or malloc.h
• x=(data_type *)malloc(n*sizeof(data_type));
#include<stdio.h>
int SUM(int n,int *x)
{
int i,sum=0;
for(i=0;i<n;++i){
sum=sum+ *(x+i);
}
return sum;
}
main()
{
int *x,i,n,y;
printf("how many number are u entering?");
scanf("%d",&n);
printf("n");
x=(int *)malloc(n*sizeof(int));
for(i=0;i<n;++i)
{
scanf("%d",x+i);
}
y=SUM(n,x);
printf("sum of %d nos is %d",n,y);
}
printf("sum of %d nos is %d",n,y);
}
OPERATION ON POINTERS
#include<stdio.h>
main()
{
int *px;
int i=1;
float f=0.3;
float d=0.005;
char c= '*';
px=&i;
printf("Value: i:%i f:%f d:%f c:%cn",i,f,d,c);
printf("Addresses: &i:%x &f:%x &d:%x &c:%xn",&i,&f,&d,&c);
printf("Pointer values: px:%x px+1:%x px+2:%x
px+3:%x",px,px+1,px+2,px+3);
}
Contd…
#include<stdio.h>
main()
{
int *px, *py;
int a[6] = {1,2,3,4,5,6};int a[6] = {1,2,3,4,5,6};
px=&a[0];
py=&a[5];
printf("a[0]:%x a[5]:%x px:%x py:%xn",&a[0],&a[5],px,py);
printf("*py-*px:%dn",*py-*px);
}
POINTERS AND
MULTIDIMENSIONAL ARRAYS
• multidimensional array can also be represented
with an pointer notation
• two-dimensional array, for example, is actually
a collection of one-dimensional arraysa collection of one-dimensional arrays
• we can define a two-dimensional array as a
pointer to a group of contiguous one-
dimensional arrays
Contd…
• a two-dimensional array declaration can be
written as
data- type ( *ptvar) [expression 2] ;data- type ( *ptvar) [expression 2] ;
rather than
data- type array[expression 1] [ expression 2];
Contd…
“x” is a two-dimensional integer array having 10 rows
and 20 columns
int (*x)[20];int (*x)[20];
rather than
int x[10][20];
“x” is defined to be a pointer to a group of contiguous,
one-dimensional, 20-element integer arrays
Contd…
x[0]= (int *) malloc (20 * sizeof(int));
.
.
x[9]= (int *) malloc (20 * sizeof(int));x[9]= (int *) malloc (20 * sizeof(int));
Contd…
• the item in row 3,,,, column 6 can be
accessed by writing either
a[2][5]a[2][5]
or
* ( * ( x* ( * ( x* ( * ( x* ( * ( x + 2)2)2)2) + 5)5)5)5)
matrix addition using pointers
• *(*(c+row)+col)
=
*(*(a+row)+col)+*(*(b+row)+col)
#include<stdio.h>
#define MAXROWS 20
void readinput(int *a[MAXROWS], int nrows, int ncols);
void computesums(int *a[MAXROWS],int *b[MAXROWS],int *c[MAXROWS],int nrows,int ncols);
void writeoutput(int *c[MAXROWS],int nrows,int ncols);
main()
{
int row,nrows,ncols;
int *a[MAXROWS],*b[MAXROWS],*c[MAXROWS];
printf("How many rows?");
scanf("%d",&nrows);
printf("n How many columns");
scanf("%d",&ncols);
for(row=0;row<nrows;++row){
a[row]=(int *)malloc(ncols*sizeof(int));
b[row]=(int *)malloc(ncols*sizeof(int));
c[row]=(int *)malloc(ncols*sizeof(int));
}
printf("nnFirst table:n");
readinput(a,nrows,ncols);
printf("nnSecond table:n");
readinput(b,nrows,ncols);
computesums(a,b,c,nrows,ncols);
printf("nnSums of the elements:nn");
writeoutput(c,nrows,ncols);writeoutput(c,nrows,ncols);
}
void readinput(int *a[MAXROWS],int m,int n)
{
int row, col;
for(row=0;row<m;++row){
for(col=0;col<n;++col){
scanf("%d",(*(a+row)+col));
}
}
}
void computesums(int *a[MAXROWS],int *b[MAXROWS],int *c[MAXROWS],int m,int n)
{
int row, col;
for(row=0;row<m;++row){
for(col=0;col<n;++col){
*(*(c+row)+col) = *(*(a+row)+col)+*(*(b+row)+col);
}
}
}
void writeoutput(int *a[MAXROWS],int m,int n)
{
int row, col;
for(row=0;row<m;++row){
for(col=0;col<n;++col){
printf("%dt",*(*(a+row)+col));
#include<stdio.h>
#define MAXROWS 20
void readinput(int *a[MAXROWS], int nrows, int
ncols);
void computesums(int *a[MAXROWS],intvoid computesums(int *a[MAXROWS],int
*b[MAXROWS],int *c[MAXROWS],int nrows,int
ncols);
void writeoutput(int *c[MAXROWS],int nrows,int
ncols);
main()
{
int row,nrows,ncols;
int *a[MAXROWS],*b[MAXROWS],*c[MAXROWS];
printf("How many rows?");
scanf("%d",&nrows);
printf("n How many columns");
scanf("%d",&ncols);
for(row=0;row<nrows;++row){
a[row]=(int *)malloc(ncols*sizeof(int));
b[row]=(int *)malloc(ncols*sizeof(int));
c[row]=(int *)malloc(ncols*sizeof(int));c[row]=(int *)malloc(ncols*sizeof(int));
}
printf("nnFirst table:n");
readinput(a,nrows,ncols);
printf("nnSecond table:n");
readinput(b,nrows,ncols);
computesums(a,b,c,nrows,ncols);
printf("nnSums of the elements:nn");
writeoutput(c,nrows,ncols);
}
void readinput(int *a[MAXROWS],int m,int
n)
{
int row, col;
for(row=0;row<m;++row){
for(col=0;col<n;++col){for(col=0;col<n;++col){
scanf("%d",(*(a+row)+col));
}
}
}
void computesums(int *a[MAXROWS],int
*b[MAXROWS],int *c[MAXROWS],int m,int n)
{
int row, col;
for(row=0;row<m;++row){
for(col=0;col<n;++col){
*(*(c+row)+col) =*(*(c+row)+col) =
*(*(a+row)+col)+*(*(b+row)+col);
}
}
}
void writeoutput(int *a[MAXROWS],int m,int
n)
{
int row, col;
for(row=0;row<m;++row){
for(col=0;col<n;++col){
printf("%dt",*(*(a+row)+col));printf("%dt",*(*(a+row)+col));
}
printf("n");
}
}

More Related Content

What's hot

C tech questions
C tech questionsC tech questions
C tech questionsvijay00791
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer웅식 전
 
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
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersKimikazu Kato
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in CTushar B Kute
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Ali Aminian
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 

What's hot (20)

Arrays
ArraysArrays
Arrays
 
C tech questions
C tech questionsC tech questions
C tech questions
 
Arrays
ArraysArrays
Arrays
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Pointers_c
Pointers_cPointers_c
Pointers_c
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
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
 
Introduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning ProgrammersIntroduction to NumPy for Machine Learning Programmers
Introduction to NumPy for Machine Learning Programmers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in C
 
Pointers and arrays
Pointers and arraysPointers and arrays
Pointers and arrays
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
Pointer
PointerPointer
Pointer
 

Similar to Pointers [compatibility mode]

Similar to Pointers [compatibility mode] (20)

Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Array
ArrayArray
Array
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
Session 4
Session 4Session 4
Session 4
 
Chapter09-10.PPT
Chapter09-10.PPTChapter09-10.PPT
Chapter09-10.PPT
 
Chapter09-10 Pointers and operations .PPT
Chapter09-10  Pointers and operations .PPTChapter09-10  Pointers and operations .PPT
Chapter09-10 Pointers and operations .PPT
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 
Arrays
ArraysArrays
Arrays
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
Array
ArrayArray
Array
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 

More from Kathmandu University (6)

Storage class
Storage classStorage class
Storage class
 
structure
structurestructure
structure
 
Function
Function Function
Function
 
Looping
LoopingLooping
Looping
 
control statement
control statement control statement
control statement
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 

Recently uploaded

Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
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
 
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
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
“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
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
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
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Recently uploaded (20)

Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
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
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
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🔝
 
“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...
 
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
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
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
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
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🔝
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 

Pointers [compatibility mode]

  • 2. Pointer • A pointer is a variable that represents the location (rather than the value) of a data item • Indirect Variable • pointers can be used to pass information back• pointers can be used to pass information back and forth between a function and its reference point • pointers provide a way to return multiple data items from a function via function arguments
  • 3. Contd… • pointers also permit references to other functions to be specified as arguments to a given function • pointers are also closely associated with• pointers are also closely associated with arrays and therefore provide an alternate way to access individual array elements • int *u u is a pointer that store address
  • 4. #include<stdio.h> main( ) { int u=5; int *pu; pu=&u;pu=&u; printf("n u=%d &u=%x pu=%x *pu=%d",u,&u,pu,*pu); }
  • 5. #include<stdio.h> main() { int u=5; int *pu; printf("n u=%d &u=%x pu=%xprintf("n u=%d &u=%x pu=%x *pu=%d",u,&u,pu,*pu); } *pu and pu give garbage value
  • 6. #include<stdio.h> main() { int u=5; int *pu=6; printf("n u=%d &u=%x pu=%x *pu=%d",u,&u,pu,*pu);*pu=%d",u,&u,pu,*pu); } Non portable pointer conversion u=5, &u=fff4, pu=6, *pu=27762
  • 7. #include<stdio.h> main() { int u=5; int *pu; pu=&u; printf("n u=%d &u=%x pu=%x *pu=%d",u,&u,pu,*pu); *pu=u+2;*pu=u+2; printf("n u=%d &u=%x pu=%x *pu=%d",u,&u,pu,*pu); u=*pu*5; printf("n u=%d &u=%x pu=%x *pu=%d",u,&u,pu,*pu); }
  • 8. PASSING POINTERS TO A FUNCTION • passing pointer as an argument to the function allows data items within the calling portion of the program to be accessed by the function, altered within the function, and then returned to the calling portion of the program in altered formin altered form • passing pointers as arguments is known as passing by reference (or by address or by location),in contrast to passing arguments by value
  • 9. void passingbyvalue(int u,int v) { u=0, v=0; printf("n Within passingbyvalue function: u=%d v=%d",u,v); return; } void passingbyref(int *pu,int *pv) { *pu=0, *pv=0; printf("n Within passingbyref main() { int u=5,v=6; printf("nBefore calling function passingbyvalue: u=%d v=%d",u,v); passingbyvalue(u,v); printf("nAfter calling function passingbyvalue: u=%d v=%d",u,v); printf("nnBefore calling function passingbyref: u=%d v=%d",u,v); printf("n Within passingbyref function: *pu=%d *pv=%d",*pu,*pv); return; } passingbyref(&u,&v); printf("nAfter calling function passingbyref: u=%d v=%d",u,v); }
  • 10. #include<stdio.h> #include<ctype.h> void scan_line(char line[],int *pv,int *pc,int *pd,int *pw,int *po); main() { char line[80]; int vowels=0,consonants=0,digits=0,whi tespc=0,other=0; printf("Enter a line of text below:n"); scanf("%[^n]",line); scan_line(line,&vowels,&consonants,&d igits,&whitespc,&other); void scan_line(char line[],int *pv,int *pc,int *pd,int *pw,int *po) { char c; int count=0; while ((c=toupper(line[count]))!='0'){ if(c=='A'|| c=='E'|| c=='I'||c=='O'|| c=='U') ++*pv; else if(c>='A' && c<='Z') ++*pc; else if(c>='0' && c<='9') ++*pd; else if(c ==' ' || c == 't') ++*pw; elseigits,&whitespc,&other); printf("nNo. of vowels:%d",vowels); printf("nNo. of consonants:%d",consonants); printf("nNo. of digits:%d",digits); printf("nNo. of whitespace characters:%d",whitespc); printf("nNo. of other characters:%d",other); } ++*po; ++count; } return; }
  • 11. POINTERS AND ONE-DIMENSIONAL ARRAYS #include<stdio.h> char x[ ] = "This string is declared externallyn"; main() { static char y[ ] = "This string is declared within main"; #include<stdio.h> char *x= "This string is declared externallyn"; main() { char *y = "This string is declared within main"; printf("%s",x); printf("%s",y); }declared within main"; printf("%s",x); printf("%s",y); } }
  • 12. Contd… • an array name is a pointer to the first element in the array • x is a one dimensional array, then the address of the first array element can be expressed asof the first array element can be expressed as either &x[0] or simply as x
  • 13. #include<stdio.h> main() { int x[5]={1,2,3,4,5},i; for(i=0;i<5;++i){ printf("ni=%d x[%d]=%d *(x+%d)=%d",i,i,x[i],i,*(x+i));printf("ni=%d x[%d]=%d *(x+%d)=%d",i,i,x[i],i,*(x+i)); printf(" &x[%d]=%x x+%d=%x",i,&x[i],i,(x+i)); } }
  • 14. DYNAMIC MEMORY ALLOCATION • an array name is actually a pointer to the first element within the array, it should be possible to define the array as a pointer variable rather than as a conventional array • a conventional array definition results in a fixed block of memory being reserved at the beginning of program execution, whereas this does not occur if the array is represented in terms of a pointer variable
  • 15. Contd… • the use of a pointer variable to represent an array requires some type of initial memory assignment before the array elements are processed • known as dynamic memory allocation • the malloc library function is used for this purpose • stdlib.h or malloc.h• stdlib.h or malloc.h • x=(data_type *)malloc(n*sizeof(data_type));
  • 16. #include<stdio.h> int SUM(int n,int *x) { int i,sum=0; for(i=0;i<n;++i){ sum=sum+ *(x+i); } return sum; } main() { int *x,i,n,y; printf("how many number are u entering?"); scanf("%d",&n); printf("n"); x=(int *)malloc(n*sizeof(int)); for(i=0;i<n;++i) { scanf("%d",x+i); } y=SUM(n,x); printf("sum of %d nos is %d",n,y); } printf("sum of %d nos is %d",n,y); }
  • 17. OPERATION ON POINTERS #include<stdio.h> main() { int *px; int i=1; float f=0.3; float d=0.005; char c= '*'; px=&i; printf("Value: i:%i f:%f d:%f c:%cn",i,f,d,c); printf("Addresses: &i:%x &f:%x &d:%x &c:%xn",&i,&f,&d,&c); printf("Pointer values: px:%x px+1:%x px+2:%x px+3:%x",px,px+1,px+2,px+3); }
  • 18. Contd… #include<stdio.h> main() { int *px, *py; int a[6] = {1,2,3,4,5,6};int a[6] = {1,2,3,4,5,6}; px=&a[0]; py=&a[5]; printf("a[0]:%x a[5]:%x px:%x py:%xn",&a[0],&a[5],px,py); printf("*py-*px:%dn",*py-*px); }
  • 19. POINTERS AND MULTIDIMENSIONAL ARRAYS • multidimensional array can also be represented with an pointer notation • two-dimensional array, for example, is actually a collection of one-dimensional arraysa collection of one-dimensional arrays • we can define a two-dimensional array as a pointer to a group of contiguous one- dimensional arrays
  • 20. Contd… • a two-dimensional array declaration can be written as data- type ( *ptvar) [expression 2] ;data- type ( *ptvar) [expression 2] ; rather than data- type array[expression 1] [ expression 2];
  • 21. Contd… “x” is a two-dimensional integer array having 10 rows and 20 columns int (*x)[20];int (*x)[20]; rather than int x[10][20]; “x” is defined to be a pointer to a group of contiguous, one-dimensional, 20-element integer arrays
  • 22. Contd… x[0]= (int *) malloc (20 * sizeof(int)); . . x[9]= (int *) malloc (20 * sizeof(int));x[9]= (int *) malloc (20 * sizeof(int));
  • 23. Contd… • the item in row 3,,,, column 6 can be accessed by writing either a[2][5]a[2][5] or * ( * ( x* ( * ( x* ( * ( x* ( * ( x + 2)2)2)2) + 5)5)5)5)
  • 24. matrix addition using pointers • *(*(c+row)+col) = *(*(a+row)+col)+*(*(b+row)+col)
  • 25. #include<stdio.h> #define MAXROWS 20 void readinput(int *a[MAXROWS], int nrows, int ncols); void computesums(int *a[MAXROWS],int *b[MAXROWS],int *c[MAXROWS],int nrows,int ncols); void writeoutput(int *c[MAXROWS],int nrows,int ncols); main() { int row,nrows,ncols; int *a[MAXROWS],*b[MAXROWS],*c[MAXROWS]; printf("How many rows?"); scanf("%d",&nrows); printf("n How many columns"); scanf("%d",&ncols); for(row=0;row<nrows;++row){ a[row]=(int *)malloc(ncols*sizeof(int)); b[row]=(int *)malloc(ncols*sizeof(int)); c[row]=(int *)malloc(ncols*sizeof(int)); } printf("nnFirst table:n"); readinput(a,nrows,ncols); printf("nnSecond table:n"); readinput(b,nrows,ncols); computesums(a,b,c,nrows,ncols); printf("nnSums of the elements:nn"); writeoutput(c,nrows,ncols);writeoutput(c,nrows,ncols); } void readinput(int *a[MAXROWS],int m,int n) { int row, col; for(row=0;row<m;++row){ for(col=0;col<n;++col){ scanf("%d",(*(a+row)+col)); } } } void computesums(int *a[MAXROWS],int *b[MAXROWS],int *c[MAXROWS],int m,int n) { int row, col; for(row=0;row<m;++row){ for(col=0;col<n;++col){ *(*(c+row)+col) = *(*(a+row)+col)+*(*(b+row)+col); } } } void writeoutput(int *a[MAXROWS],int m,int n) { int row, col; for(row=0;row<m;++row){ for(col=0;col<n;++col){ printf("%dt",*(*(a+row)+col));
  • 26. #include<stdio.h> #define MAXROWS 20 void readinput(int *a[MAXROWS], int nrows, int ncols); void computesums(int *a[MAXROWS],intvoid computesums(int *a[MAXROWS],int *b[MAXROWS],int *c[MAXROWS],int nrows,int ncols); void writeoutput(int *c[MAXROWS],int nrows,int ncols);
  • 27. main() { int row,nrows,ncols; int *a[MAXROWS],*b[MAXROWS],*c[MAXROWS]; printf("How many rows?"); scanf("%d",&nrows); printf("n How many columns"); scanf("%d",&ncols); for(row=0;row<nrows;++row){ a[row]=(int *)malloc(ncols*sizeof(int)); b[row]=(int *)malloc(ncols*sizeof(int)); c[row]=(int *)malloc(ncols*sizeof(int));c[row]=(int *)malloc(ncols*sizeof(int)); } printf("nnFirst table:n"); readinput(a,nrows,ncols); printf("nnSecond table:n"); readinput(b,nrows,ncols); computesums(a,b,c,nrows,ncols); printf("nnSums of the elements:nn"); writeoutput(c,nrows,ncols); }
  • 28. void readinput(int *a[MAXROWS],int m,int n) { int row, col; for(row=0;row<m;++row){ for(col=0;col<n;++col){for(col=0;col<n;++col){ scanf("%d",(*(a+row)+col)); } } }
  • 29. void computesums(int *a[MAXROWS],int *b[MAXROWS],int *c[MAXROWS],int m,int n) { int row, col; for(row=0;row<m;++row){ for(col=0;col<n;++col){ *(*(c+row)+col) =*(*(c+row)+col) = *(*(a+row)+col)+*(*(b+row)+col); } } }
  • 30. void writeoutput(int *a[MAXROWS],int m,int n) { int row, col; for(row=0;row<m;++row){ for(col=0;col<n;++col){ printf("%dt",*(*(a+row)+col));printf("%dt",*(*(a+row)+col)); } printf("n"); } }