SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Downloaden Sie, um offline zu lesen
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");
}
}

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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
 

Ähnlich wie Pointers [compatibility mode]

Ähnlich wie 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
 

Mehr von 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
 

Kürzlich hochgeladen

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 

Kürzlich hochgeladen (20)

FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

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"); } }