SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
CS110 - Intro. To Computing
Pointers !!!!!!!
Lab - 5
• Last Monday’s batch - come today
• Last Friday’s batch - class cancelled
because of server problem - come
tomorrow - 2 PM
Let us Revise
• What are these?
– int *p, *q, *r;

• What are these?
– int a,b,c;
Let us Revise
• What are these?
– int *p, *q, *r; - p, q, r are address storing
integer variables

• What are these?
– int a,b,c; - a, b, c are names of integer
variables
Let us Revise
• What are these?
– int *p, *q, *r;
– *p = 5; Content of address p is 5.

• What are these?
– int a,b,c;
– &a = 0x1000; Address storing variable
named a is 0x1000
Let us Revise
• Given int *p;
– &p is wrong - It means address of address.

• Given int b;
– *b is wrong - it means value of value
Let us Revise
• Function Calls
• Given int my_func(int *p);
• This can be called as follows:
– int a,b; b = my_func(&a);
– int *a; int b; b = my_func(a);
– Int *a, *b; *b = my_func(a);
Let us Revise
• Function Calls
• Given int my_func(int p);
• This can be called as follows:
– int a,b; b = my_func(a);
– int *a; int b; b = my_func(*a);
– Int *a, *b; *b = my_func(*a);
Let us Revise
• Arrays
– int a[100];

• Now a is a pointer to the start of the
location - address of a variable
• a[5] is a value of the 5th location - name
of a variable
Passing Arrays
• All our previous functions we passed
only single values - not arrays.
• In C array is passed “by reference”.
– Implication
• If you change the value inside the function it
gets reflected in the calling function.
Example
void modify(int a[]) {
int count;
printf(“n From the function, after modifying the values:n”);
for (count = 0; count <= 2; count++) {
a[count] = -9;
printf(“a[%d] = %dn”, count, a[count]);
}
return;
}
main() {
int count, a[3];
void modify(int a[]);
printf(“n From main, before calling the
functionn”);
for (count = 0; count <= 2; ++count) {
a[count] = count + 1;
printf(“a[%d] = %dn”,count, a[count]);
}
modify();
printf(“n From main, after calling the
functionn”);
for (count = 0; count <= 2; ++count) {
printf(“a[%d] = %dn”,count, a[count]);
}
}
From main, before calling the function:
a[0] = 1
a[1] = 2
a[2] = 3

main() {
int count, a[3];
void modify(int a[]);
printf(“n From main, before calling the
functionn”);
for (count = 0; count <= 2; ++count) {
a[count] = count + 1;
printf(“a[%d] = %dn”,count, a[count]);
}
modify();
printf(“n From main, before calling the
functionn”);
for (count = 0; count <= 2; ++count) {
printf(“a[%d] = %dn”,count, a[count]);
}
}
Example
void modify(int a[]) {
int count;
printf(“n From the function, after modifying the values:n”);
for (count = 0; count <= 2; count++) {
a[count] = -9;
printf(“a[%d] = %dn”, count, a[count]);
}
return;
}

From the function, after modifying the values:
a[0] = -9
a[1] = -9
a[2] = -9
main() {
int count, a[3];
void modify(int a[]);
printf(“n From main, before calling the
functionn”);
for (count = 0; count <= 2; ++count) {
a[count] = count + 1;
printf(“a[%d] = %dn”,count, a[count]);
}
modify(a);
printf(“n From main, after calling the
functionn”);
for (count = 0; count <= 2; ++count) {
printf(“a[%d] = %dn”,count, a[count]);
}
From main, after calling the function:
}

a[0] = -9
a[1] = -9
a[2] = -9
Multidimensional Arrays
main() {
int *nrows, *ncols;
int a[30][30];
void readinput(int a[][30], int *,int *);
void writeoutput(int a[][30], int, int);
readinput(a,nrows,ncols);
printf(“n Number of rows %dn”,*nrows);
printf(“n Number of columns %dn”,*ncols);
writeoutput(a,*nows,*ncols);
}
void readinput(int a[][30], int *m, int *n) {
int i, j;
printf(“Number of rowsn”);
scanf(“%d”,m);
printf(“Number of columnsn”);
scanf(“%d”,n);
for (i = 0; i < *m; i++)
for (j = 0; j < *n; j++)
scanf(“%d”, &a[i][j])
}
void writeoutput(int a[][30], int row, int col) {
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++)
printf(“%4d”, a[i][j]);
printf(“n”);
}
return;
}
int u = 3;
int v;
int *pu;
int *pv;
pu = &u;
v = *pu;
pv = &v;
When you print * and & versions of them? What happens?

Contenu connexe

Tendances

Call by value
Call by valueCall by value
Call by value
Dharani G
 

Tendances (20)

Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 
Call by value
Call by valueCall by value
Call by value
 
C programming function
C  programming functionC  programming function
C programming function
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
functions of C++
functions of C++functions of C++
functions of C++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function (rule in programming)
Function (rule in programming)Function (rule in programming)
Function (rule in programming)
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
Functions in C
Functions in CFunctions in C
Functions in C
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
Function Pointer in C
Function Pointer in CFunction Pointer in C
Function Pointer in C
 
Functions in c++,
Functions in c++,Functions in c++,
Functions in c++,
 
Functions
FunctionsFunctions
Functions
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

En vedette (7)

Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational Engineering
 
Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational Engineering
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational Engineering
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational Engineering
 
Lec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringLec19-CS110 Computational Engineering
Lec19-CS110 Computational Engineering
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational Engineering
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational Engineering
 

Similaire à Lec21-CS110 Computational Engineering

booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
GkhanGirgin3
 

Similaire à Lec21-CS110 Computational Engineering (20)

46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 
parameter passing in c#
parameter passing in c#parameter passing in c#
parameter passing in c#
 
Array Cont
Array ContArray Cont
Array Cont
 
L4 functions
L4 functionsL4 functions
L4 functions
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
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
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Functions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptxFunctions IN CPROGRAMMING OF ENGINEERING.pptx
Functions IN CPROGRAMMING OF ENGINEERING.pptx
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
3 Function & Storage Class.pptx
3 Function & Storage Class.pptx3 Function & Storage Class.pptx
3 Function & Storage Class.pptx
 

Plus de Sri Harsha Pamu

Plus de Sri Harsha Pamu (16)

Lec13
Lec13Lec13
Lec13
 
Lec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringLec09-CS110 Computational Engineering
Lec09-CS110 Computational Engineering
 
Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational Engineering
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational Engineering
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational Engineering
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational Engineering
 
Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational Engineering
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational Engineering
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational Engineering
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational Engineering
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational Engineering
 
Android..imp google
Android..imp googleAndroid..imp google
Android..imp google
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability study
 
Android gui framework
Android gui frameworkAndroid gui framework
Android gui framework
 
Hackernote on gsoc
Hackernote on gsocHackernote on gsoc
Hackernote on gsoc
 
Boot2Gecko Hackernote
Boot2Gecko HackernoteBoot2Gecko Hackernote
Boot2Gecko Hackernote
 

Dernier

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 

Dernier (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

Lec21-CS110 Computational Engineering

  • 1. CS110 - Intro. To Computing Pointers !!!!!!!
  • 2. Lab - 5 • Last Monday’s batch - come today • Last Friday’s batch - class cancelled because of server problem - come tomorrow - 2 PM
  • 3. Let us Revise • What are these? – int *p, *q, *r; • What are these? – int a,b,c;
  • 4. Let us Revise • What are these? – int *p, *q, *r; - p, q, r are address storing integer variables • What are these? – int a,b,c; - a, b, c are names of integer variables
  • 5. Let us Revise • What are these? – int *p, *q, *r; – *p = 5; Content of address p is 5. • What are these? – int a,b,c; – &a = 0x1000; Address storing variable named a is 0x1000
  • 6. Let us Revise • Given int *p; – &p is wrong - It means address of address. • Given int b; – *b is wrong - it means value of value
  • 7. Let us Revise • Function Calls • Given int my_func(int *p); • This can be called as follows: – int a,b; b = my_func(&a); – int *a; int b; b = my_func(a); – Int *a, *b; *b = my_func(a);
  • 8. Let us Revise • Function Calls • Given int my_func(int p); • This can be called as follows: – int a,b; b = my_func(a); – int *a; int b; b = my_func(*a); – Int *a, *b; *b = my_func(*a);
  • 9. Let us Revise • Arrays – int a[100]; • Now a is a pointer to the start of the location - address of a variable • a[5] is a value of the 5th location - name of a variable
  • 10. Passing Arrays • All our previous functions we passed only single values - not arrays. • In C array is passed “by reference”. – Implication • If you change the value inside the function it gets reflected in the calling function.
  • 11. Example void modify(int a[]) { int count; printf(“n From the function, after modifying the values:n”); for (count = 0; count <= 2; count++) { a[count] = -9; printf(“a[%d] = %dn”, count, a[count]); } return; }
  • 12. main() { int count, a[3]; void modify(int a[]); printf(“n From main, before calling the functionn”); for (count = 0; count <= 2; ++count) { a[count] = count + 1; printf(“a[%d] = %dn”,count, a[count]); } modify(); printf(“n From main, after calling the functionn”); for (count = 0; count <= 2; ++count) { printf(“a[%d] = %dn”,count, a[count]); } }
  • 13. From main, before calling the function: a[0] = 1 a[1] = 2 a[2] = 3 main() { int count, a[3]; void modify(int a[]); printf(“n From main, before calling the functionn”); for (count = 0; count <= 2; ++count) { a[count] = count + 1; printf(“a[%d] = %dn”,count, a[count]); } modify(); printf(“n From main, before calling the functionn”); for (count = 0; count <= 2; ++count) { printf(“a[%d] = %dn”,count, a[count]); } }
  • 14. Example void modify(int a[]) { int count; printf(“n From the function, after modifying the values:n”); for (count = 0; count <= 2; count++) { a[count] = -9; printf(“a[%d] = %dn”, count, a[count]); } return; } From the function, after modifying the values: a[0] = -9 a[1] = -9 a[2] = -9
  • 15. main() { int count, a[3]; void modify(int a[]); printf(“n From main, before calling the functionn”); for (count = 0; count <= 2; ++count) { a[count] = count + 1; printf(“a[%d] = %dn”,count, a[count]); } modify(a); printf(“n From main, after calling the functionn”); for (count = 0; count <= 2; ++count) { printf(“a[%d] = %dn”,count, a[count]); } From main, after calling the function: } a[0] = -9 a[1] = -9 a[2] = -9
  • 16. Multidimensional Arrays main() { int *nrows, *ncols; int a[30][30]; void readinput(int a[][30], int *,int *); void writeoutput(int a[][30], int, int); readinput(a,nrows,ncols); printf(“n Number of rows %dn”,*nrows); printf(“n Number of columns %dn”,*ncols); writeoutput(a,*nows,*ncols); }
  • 17. void readinput(int a[][30], int *m, int *n) { int i, j; printf(“Number of rowsn”); scanf(“%d”,m); printf(“Number of columnsn”); scanf(“%d”,n); for (i = 0; i < *m; i++) for (j = 0; j < *n; j++) scanf(“%d”, &a[i][j]) }
  • 18. void writeoutput(int a[][30], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) printf(“%4d”, a[i][j]); printf(“n”); } return; }
  • 19. int u = 3; int v; int *pu; int *pv; pu = &u; v = *pu; pv = &v; When you print * and & versions of them? What happens?