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

Weitere ähnliche Inhalte

Was ist angesagt?

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

Andere mochten auch

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
 

Andere mochten auch (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
 

Ähnlich wie Lec23-CS110 Computational Engineering

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

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

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

Kürzlich hochgeladen

4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptxDhatriParmar
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 

Kürzlich hochgeladen (20)

4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
Unraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptxUnraveling Hypertext_ Analyzing  Postmodern Elements in  Literature.pptx
Unraveling Hypertext_ Analyzing Postmodern Elements in Literature.pptx
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 

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;