SlideShare a Scribd company logo
1 of 17
Download to read offline
Pointers - Continued
V. Kamakoti
Let us revise
#include <stdio.h>
main() {
What is the output?
int u1, u2;
int v = 3;
int *pv;
u1 = 2* (v + 5);
pv = &v;
u2 = 2*(*pv + 5);
printf(“n u1=%d u2=%d”,u1,u2);
}
Let us revise
#include <stdio.h>
main() {
u1 = 16 u2 = 16
int u1, u2;
int v = 3;
int *pv;
u1 = 2* (v + 5);
pv = &v;
u2 = 2*(*pv + 5);
printf(“n u1=%d u2=%d”,u1,u2);
}
Let us revise
char my_word[50];
int a[100];
scanf(“%s %d”,my_word,&a[50]);
Note: No “&” for arrays, but “&” required for
reading an element of an array
Passing partial arrays
#include<stdio.h>
main() {
int z[100];
void process(int z[]);
process(&z[50]);
print(“%d %d”,z[50],z[51]);
}
What is the output?

void process(int a[]) {
a[0] = 5;
a[1] = 6;
}
Passing partial arrays
#include<stdio.h>
main() {
int z[100];
void process(int z[]);
process(&z[50]);
print(“%d %d”,z[50],z[51]);
}
56

void process(int a[]) {
a[0] = 5;
a[1] = 6;
}
Functions can return pointers
main() {
int z[100];
int *a;
int *scan(int b[]);
a = scan(z);
printf(“%d %d”,a[0],a[1]);
}
What is the output?

int *scan(int b[]) {
int *c;
b[0] = 5;
b[1] = 6;
c = b;
return( c );
}
Functions can return pointers
main() {
int z[100];
int *a;
int *scan(int b[]);
a = scan(z);
printf(“%d %d”,a[0],a[1]);
}

int *scan(int b[]) {
int *c;
b[0] = 5;
b[1] = 6;
c = b;
return( c );
}

56
Functions can return pointers
main() {
int z[100];
int *a;
int *scan(int b[]);
a = scan(z);
z[3] = 8;
printf(“Output: %d”,a[3]);
}

int *scan(int b[]) {
int *c;
b[0] = 5;
b[1] = 6;
c = b;
return( c );
}

What is the Output?
Functions can return pointers
main() {
int z[100];
int *a;
int *scan(int b[]);
a = scan(z);
z[3] = 8;
printf(“Output: %d”,a[3]);
}

int *scan(int b[]) {
int *c;
b[0] = 5;
b[1] = 6;
c = b;
return( c );
}

Output: 8
Pointers and 1-D arrays
•
•
•
•
•

int x[100];
x : pointer to x[0] - say address 1002
x + 1: pointer to x[1] - address 1006
x + 2: pointer to x[2] - address 1010
x + i: pointer to x[i]
= 1000 + i * sizeof(int);
= Base Address + i * sizeof(int);
What happens
main() {
int z[100];
scanf(“%d”, z+5);
printf(“%d %d”,z[5],*(z+5));
}
If you enter 10, the code outputs
10 10
What is missing?
• You should know the size of the array
while writing the code
• Else space allocation cannot be done
• It is a limitation to the user of the code
• Any solution?
– Yes - through Dynamic Memory Allocation
These are Equivalent
• int x[10];
• Another way
– #define SIZE 10
– int x[SIZE]

• int x[10] = {1,2,3,5,6,7,8,9,10,11};
• int x[] = {1,2,3,4,5,6,7,8,9,10,11};
• Another way
– int *x;
– x = (int *) malloc(10 * sizeof(int));
These are Equivalent
– int *x;
– x = malloc(10 * sizeof(int));
– This shall return a pointer to character inconsistent with definition of “x” - which is an
integer pointer.
– x = (int *) malloc(10* sizeof(int));
– This is called “Pointer Casting”.
Look at this
main() {
int p;
int *q;
printf(“Enter size of arrayn”);
scanf(“%d”,&p);
q = (int *) malloc(p * sizeof(int));
}
Creates an array of size as desired by user.
So What?
int line[80];
int *pl;
line[2] = line[1]; //is equivalent to
*(line + 2) = line[1];
*(line + 2) = *(line + 1);
line[2] = *(line + 1);
p1 = &line[1]; //Is equivalent to
p1 = line + 1;

More Related Content

What's hot

What's hot (20)

4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functions
 
Program presentation
Program presentationProgram presentation
Program presentation
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
SPL 10.1 | Manual Tracing on Basic Loop & 1D Array
SPL 10.1 | Manual Tracing on Basic Loop & 1D ArraySPL 10.1 | Manual Tracing on Basic Loop & 1D Array
SPL 10.1 | Manual Tracing on Basic Loop & 1D Array
 
Lec04-CS110 Computational Engineering
Lec04-CS110 Computational EngineeringLec04-CS110 Computational Engineering
Lec04-CS110 Computational Engineering
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
 
Perceptron
PerceptronPerceptron
Perceptron
 
6. function
6. function6. function
6. function
 
Stack array
Stack arrayStack array
Stack array
 
Java Programming Workshop
Java Programming WorkshopJava Programming Workshop
Java Programming Workshop
 
computer graphics practicals
computer graphics practicalscomputer graphics practicals
computer graphics practicals
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
 
cosc 281 hw2
cosc 281 hw2cosc 281 hw2
cosc 281 hw2
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Data structure
Data structureData structure
Data structure
 
C programs
C programsC programs
C programs
 

Viewers also liked

Lec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringLec19-CS110 Computational Engineering
Lec19-CS110 Computational EngineeringSri Harsha Pamu
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringSri Harsha Pamu
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringSri Harsha Pamu
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringSri Harsha Pamu
 
Lec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringLec16-CS110 Computational Engineering
Lec16-CS110 Computational EngineeringSri Harsha Pamu
 
Lec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringLec10-CS110 Computational Engineering
Lec10-CS110 Computational EngineeringSri Harsha Pamu
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringSri Harsha Pamu
 

Viewers also liked (7)

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
 
Lec14-CS110 Computational Engineering
Lec14-CS110 Computational EngineeringLec14-CS110 Computational Engineering
Lec14-CS110 Computational Engineering
 
Lec15-CS110 Computational Engineering
Lec15-CS110 Computational EngineeringLec15-CS110 Computational Engineering
Lec15-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
 
Lec21-CS110 Computational Engineering
Lec21-CS110 Computational EngineeringLec21-CS110 Computational Engineering
Lec21-CS110 Computational Engineering
 

Similar to Lec23-CS110 Computational Engineering

Similar to Lec23-CS110 Computational Engineering (20)

Vcs5
Vcs5Vcs5
Vcs5
 
Vcs16
Vcs16Vcs16
Vcs16
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
array.ppt
array.pptarray.ppt
array.ppt
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
7. arrays
7. arrays7. arrays
7. arrays
 
Array
ArrayArray
Array
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02Cse115 lecture08repetitionstructures part02
Cse115 lecture08repetitionstructures part02
 
Are we ready to Go?
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
Erlang bootstrap course
Erlang bootstrap courseErlang bootstrap course
Erlang bootstrap course
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
Unix Programs
Unix ProgramsUnix Programs
Unix Programs
 

More from Sri Harsha Pamu

Lec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringLec09-CS110 Computational Engineering
Lec09-CS110 Computational EngineeringSri Harsha Pamu
 
Lec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringLec08-CS110 Computational Engineering
Lec08-CS110 Computational EngineeringSri Harsha Pamu
 
Lec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringLec07-CS110 Computational Engineering
Lec07-CS110 Computational EngineeringSri Harsha Pamu
 
Lec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringLec06-CS110 Computational Engineering
Lec06-CS110 Computational EngineeringSri Harsha Pamu
 
Lec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringLec03-CS110 Computational Engineering
Lec03-CS110 Computational EngineeringSri Harsha Pamu
 
Lec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringLec02-CS110 Computational Engineering
Lec02-CS110 Computational EngineeringSri Harsha Pamu
 
Lec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringLec01-CS110 Computational Engineering
Lec01-CS110 Computational EngineeringSri Harsha Pamu
 
Lec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringLec1- CS110 Computational Engineering
Lec1- CS110 Computational EngineeringSri Harsha Pamu
 
Lec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringLec25-CS110 Computational Engineering
Lec25-CS110 Computational EngineeringSri Harsha Pamu
 
Android vulnerability study
Android vulnerability studyAndroid vulnerability study
Android vulnerability studySri Harsha Pamu
 

More from Sri Harsha Pamu (15)

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

Recently uploaded

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
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 SectorsAssociation for Project Management
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
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 SDThiyagu K
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
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 ImpactPECB
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
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 GraphThiyagu K
 

Recently uploaded (20)

Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
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
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
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
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
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
 

Lec23-CS110 Computational Engineering

  • 2. Let us revise #include <stdio.h> main() { What is the output? int u1, u2; int v = 3; int *pv; u1 = 2* (v + 5); pv = &v; u2 = 2*(*pv + 5); printf(“n u1=%d u2=%d”,u1,u2); }
  • 3. Let us revise #include <stdio.h> main() { u1 = 16 u2 = 16 int u1, u2; int v = 3; int *pv; u1 = 2* (v + 5); pv = &v; u2 = 2*(*pv + 5); printf(“n u1=%d u2=%d”,u1,u2); }
  • 4. Let us revise char my_word[50]; int a[100]; scanf(“%s %d”,my_word,&a[50]); Note: No “&” for arrays, but “&” required for reading an element of an array
  • 5. Passing partial arrays #include<stdio.h> main() { int z[100]; void process(int z[]); process(&z[50]); print(“%d %d”,z[50],z[51]); } What is the output? void process(int a[]) { a[0] = 5; a[1] = 6; }
  • 6. Passing partial arrays #include<stdio.h> main() { int z[100]; void process(int z[]); process(&z[50]); print(“%d %d”,z[50],z[51]); } 56 void process(int a[]) { a[0] = 5; a[1] = 6; }
  • 7. Functions can return pointers main() { int z[100]; int *a; int *scan(int b[]); a = scan(z); printf(“%d %d”,a[0],a[1]); } What is the output? int *scan(int b[]) { int *c; b[0] = 5; b[1] = 6; c = b; return( c ); }
  • 8. Functions can return pointers main() { int z[100]; int *a; int *scan(int b[]); a = scan(z); printf(“%d %d”,a[0],a[1]); } int *scan(int b[]) { int *c; b[0] = 5; b[1] = 6; c = b; return( c ); } 56
  • 9. Functions can return pointers main() { int z[100]; int *a; int *scan(int b[]); a = scan(z); z[3] = 8; printf(“Output: %d”,a[3]); } int *scan(int b[]) { int *c; b[0] = 5; b[1] = 6; c = b; return( c ); } What is the Output?
  • 10. Functions can return pointers main() { int z[100]; int *a; int *scan(int b[]); a = scan(z); z[3] = 8; printf(“Output: %d”,a[3]); } int *scan(int b[]) { int *c; b[0] = 5; b[1] = 6; c = b; return( c ); } Output: 8
  • 11. Pointers and 1-D arrays • • • • • int x[100]; x : pointer to x[0] - say address 1002 x + 1: pointer to x[1] - address 1006 x + 2: pointer to x[2] - address 1010 x + i: pointer to x[i] = 1000 + i * sizeof(int); = Base Address + i * sizeof(int);
  • 12. What happens main() { int z[100]; scanf(“%d”, z+5); printf(“%d %d”,z[5],*(z+5)); } If you enter 10, the code outputs 10 10
  • 13. What is missing? • You should know the size of the array while writing the code • Else space allocation cannot be done • It is a limitation to the user of the code • Any solution? – Yes - through Dynamic Memory Allocation
  • 14. These are Equivalent • int x[10]; • Another way – #define SIZE 10 – int x[SIZE] • int x[10] = {1,2,3,5,6,7,8,9,10,11}; • int x[] = {1,2,3,4,5,6,7,8,9,10,11}; • Another way – int *x; – x = (int *) malloc(10 * sizeof(int));
  • 15. These are Equivalent – int *x; – x = malloc(10 * sizeof(int)); – This shall return a pointer to character inconsistent with definition of “x” - which is an integer pointer. – x = (int *) malloc(10* sizeof(int)); – This is called “Pointer Casting”.
  • 16. Look at this main() { int p; int *q; printf(“Enter size of arrayn”); scanf(“%d”,&p); q = (int *) malloc(p * sizeof(int)); } Creates an array of size as desired by user.
  • 17. So What? int line[80]; int *pl; line[2] = line[1]; //is equivalent to *(line + 2) = line[1]; *(line + 2) = *(line + 1); line[2] = *(line + 1); p1 = &line[1]; //Is equivalent to p1 = line + 1;