SlideShare a Scribd company logo
Lectures on Numerical Methods 1
Address of a variable
Each variable that is declared is
stored in memory. Since memory is
indexed each variable has an
address.
C allows programmers to access
the address of the variables.
Unary operator & (read as ‘address
of’) gives the address of the
operand variable.
See example.
Since constants, expressions do
not have permanent storage, they
donot have address.
&1.0, &(d+6) are illegal.
See printf format.
#include <stdio.h>
#define line "t-----------------n"
main()
{
int i = 0;
char c = 'a';
short s = 1;
long l = 2;
float f = 3.0;
double d = 4.0;
printf("Variable Tablen");
printf(line);
printf("tVarttAddressttValuen");
printf(line);
printf("t%stt%pt%dn","i", &i, i);
printf("t%stt%pt%cn","c", &c, c);
printf("t%stt%pt%dn","s", &s, s);
printf("t%stt%pt%dn","l", &l, l);
printf("t%stt%pt%fn","f", &f, f);
printf("t%stt%pt%fn","d", &d, d);
printf(line);
}
Lectures on Numerical Methods 2
Address of a variable
This program was compiled by gcc on
linux machine.
The output of this program:
Variable Table
-------------------------------
Var Address Value
-------------------------------
i 0xbffffc94 0
c 0xbffffc93 a
s 0xbffffc90 1
l 0xbffffc8c 2
f 0xbffffc88 3.000000
d 0xbffffc80 4.000000
-------------------------------
#include <stdio.h>
#define line "t-----------------n"
main()
{
int i = 0;
char c = 'a';
short s = 1;
long l = 2;
float f = 3.0;
double d = 4.0;
printf("Variable Tablen");
printf(line);
printf("tVarttAddressttValuen");
printf(line);
printf("t%stt%pt%dn","i", &i, i);
printf("t%stt%pt%cn","c", &c, c);
printf("t%stt%pt%dn","s", &s, s);
printf("t%stt%pt%dn","l", &l, l);
printf("t%stt%pt%fn","f", &f, f);
printf("t%stt%pt%fn","d", &d, d);
printf(line);
}
Lectures on Numerical Methods 3
Pointer to a variable
If variables have addresses, can
these be stored in the variables
and manipulated with?
These addresses have a new data-
types (derived data-types) called
pointers.
Pointers are declared as
data-type *var-name;
In this example one pointer
variable has been declared as p
and it can store addresses of any
integer variables.
#include <stdio.h>
#define line "t----------------n"
main()
{
int i = 0;
int j = 1;
int k = 2;
float f = 3.0;
int *p = 0;
p = &k;
printf("p = %ptt*p = %dn", p, *p);
j = *p + 5;
printf(“j = %dtt*p = %dn", j, *p);
*p = *p + i;
printf("p = %ptt*p = %dn", p, *p);
p = &f;
printf("p = %ptt*p = %dn", p, *p);
}
Lectures on Numerical Methods 4
Pointer to a variable
The simplest operation is to get an
address of a variable and store it in
a pointer variable.
Example
p = &k;
p = &f;
Pointer p is declared to store an
address of an int variable.
Assigning an address of a float
variable to p, would cause a
compiler warning or error.
(Continuing in spite of warning,
may result in disaster).
#include <stdio.h>
#define line "t----------------n"
main()
{
int i = 0;
int j = 1;
int k = 2;
float f = 3.0;
int *p = 0;
p = &k;
printf("p = %ptt*p = %dn", p, *p);
j = *p + 5;
printf(“j = %dtt*p = %dn", j, *p);
*p = *p + i;
printf("p = %ptt*p = %dn", p, *p);
p = &f;
printf("p = %ptt*p = %dn", p, *p);
}
Lectures on Numerical Methods 5
Pointer to a variable
The value of pointer p is an
address of some int variable.
The value of the int variable
pointed to by p can be accessed by
using a dereferencing operator *.
Printf statement here prints the
value of p and that of *p.
p = 0xbffffc8c *p = 2
*p is just like k here. All
expressions where k can appear,
*p also can.
j = 7 *p = 2
#include <stdio.h>
#define line "t----------------n"
main()
{
int i = 4;
int j = 1;
int k = 2;
float f = 3.0;
int *p = 0;
p = &k;
printf("p = %ptt*p = %dn", p, *p);
j = *p + 5;
printf(“j = %dtt*p = %dn", j, *p);
*p = *p + i;
printf("p = %ptt*p = %dn", p, *p);
p = &f;
printf("p = %ptt*p = %dn", p, *p);
}
Lectures on Numerical Methods 6
Pointer to a variable
The dereferencing operator *p is
not only used to get the value of
the variable pointed to by p, but
also can be used to change the
value of that variable.
It is legal to use *p as left side of an
assignment.
P = 0xbffffc8c *p = 6
#include <stdio.h>
#define line "t----------------n"
main()
{
int i = 0;
int j = 1;
int k = 2;
float f = 3.0;
int *p = 0;
p = &k;
printf("p = %ptt*p = %dn", p, *p);
j = *p + 5;
printf(“j = %dtt*p = %dn", j, *p);
*p = *p + i;
printf("p = %ptt*p = %dn", p, *p);
p = &f;
printf("p = %ptt*p = %dn", p, *p);
}
Lectures on Numerical Methods 7
Pointer Arithmetic
A simple arithmetic is allowed for
the pointers.
A pointer points a variable of a
given type. When we add 1 to a
pointer variable, it points to the
next variable in the memory(though
it may not be of the same type).
Here p = p + 1 would cause the
value of p to be added 4 which is
size of int.
Look at output.
#include <stdio.h>
#define line "t----------------n"
main()
{
int i = 0;
int j = 1;
int k = 2;
int *p = 0;
p = &k;
printf("p = %ptt*p = %dn", p, *p);
p = p + 1;
printf("p = %ptt*p = %dn", p, *p);
p++ ;
printf("p = %ptt*p = %dn", p, *p);
p = &f;
printf("p = %ptt*p = %dn", p, *p);
}
Lectures on Numerical Methods 8
Pointer Arithmetic
Variable Table
----------------------------------------
Var Address Value
----------------------------------------
i 0xbffffc94 0
j 0xbffffc90 1
k 0xbffffc8c 2
f 0xbffffc88 3.000000
p 0xbffffc84 (nil)
----------------------------------------
The output of this program is given here.
p = 0xbffffc8c *p = 2
p = 0xbffffc90 *p = 1
p = 0xbffffc94 *p = 0
p = 0xbffffc88 *p = 1077936128
Lectures on Numerical Methods 9
Pointers and Functions Arguments
 We have seen that since a and b are
passed by value to swap1 function,
the values of a and b in main are not
swapped.
 But in swap2, the address of a and
address of b is passed. Hence pa and
pb contains the addresses of a and b.
Then the changes are made directly
to the locations of a and b.
 The result would be
3 5
5 3
void swap1(int a, int b) {
int temp = a;
a = b;
b = temp;
}
void swap2(int *pa, int *pb) {
int temp = *pa;
*pa = *pb;
*pb = temp;
}
main() {
int a = 3, b = 5;
swap1(a, b);
printf(“%dt%d”, a, b);
swap2(&a, &b);
printf(“%dt%d”, a, b);
}
Lectures on Numerical Methods 10
Pointers and Functions Arguments
 It is indeed inconvenient that a function can return only one value.
 Consider a function for the bisection method. The function is expected to
return a root of a function.
double getRoot(double l, double r, double tol, int maxIter);
 What would happen if the root is not bracketed in [l,r] interval? The function
is still likely to return some value which is not the required root. Ideally,
there should be one more variable indicating the error.
double getRoot(double l, double r, double tol, int maxIter, int
*error);
Lectures on Numerical Methods 11
Pointers and Arrays
An array name is a constant pointer. So if a is an array of 10 integers
and pa is a pointer to int variable, then
Pa = &a[0];
Pa = a;
Are legal and same. So are
B = a[5];
B = *(a+5);
However since a is constant pointer following is invalid
A = pa;
Ad

More Related Content

Similar to iit c prog.ppt (20)

ch08.ppt
ch08.pptch08.ppt
ch08.ppt
NewsMogul
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
Krishna Nanda
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
ShivamChaturvedi67
 
UNIT 6structureofcprogrammingforppt.pptx
UNIT 6structureofcprogrammingforppt.pptxUNIT 6structureofcprogrammingforppt.pptx
UNIT 6structureofcprogrammingforppt.pptx
jayantpatil745
 
Ch 7-pointers
Ch 7-pointersCh 7-pointers
Ch 7-pointers
Muslimee Subhany
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
Pointers
PointersPointers
Pointers
Vardhil Patel
 
Pointers are one of the core components of the C programming language.
Pointers are one of the core components of the C programming language.Pointers are one of the core components of the C programming language.
Pointers are one of the core components of the C programming language.
bhargavi804095
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c languageUNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
cricketreview
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
sajinis3
 
08-Pointers.docx An array is a linear data structure
08-Pointers.docx An array is a linear data structure08-Pointers.docx An array is a linear data structure
08-Pointers.docx An array is a linear data structure
bhargavi804095
 
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrArray in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
AnanyaSingh813245
 
Pointers
PointersPointers
Pointers
Prasadu Peddi
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
Ponters
PontersPonters
Ponters
Mukund Trivedi
 
Ponters
PontersPonters
Ponters
Mukund Trivedi
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejejeUnit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
UNIT 6structureofcprogrammingforppt.pptx
UNIT 6structureofcprogrammingforppt.pptxUNIT 6structureofcprogrammingforppt.pptx
UNIT 6structureofcprogrammingforppt.pptx
jayantpatil745
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
Pointers are one of the core components of the C programming language.
Pointers are one of the core components of the C programming language.Pointers are one of the core components of the C programming language.
Pointers are one of the core components of the C programming language.
bhargavi804095
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c languageUNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
cricketreview
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
Amit Kapoor
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
sajinis3
 
08-Pointers.docx An array is a linear data structure
08-Pointers.docx An array is a linear data structure08-Pointers.docx An array is a linear data structure
08-Pointers.docx An array is a linear data structure
bhargavi804095
 
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrArray in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
AnanyaSingh813245
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejejeUnit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 

Recently uploaded (20)

puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
Arshad Shaikh
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast BrooklynBridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
Bridging the Transit Gap: Equity Drive Feeder Bus Design for Southeast Brooklyn
i4jd41bk
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
Arshad Shaikh
 
Ad

iit c prog.ppt

  • 1. Lectures on Numerical Methods 1 Address of a variable Each variable that is declared is stored in memory. Since memory is indexed each variable has an address. C allows programmers to access the address of the variables. Unary operator & (read as ‘address of’) gives the address of the operand variable. See example. Since constants, expressions do not have permanent storage, they donot have address. &1.0, &(d+6) are illegal. See printf format. #include <stdio.h> #define line "t-----------------n" main() { int i = 0; char c = 'a'; short s = 1; long l = 2; float f = 3.0; double d = 4.0; printf("Variable Tablen"); printf(line); printf("tVarttAddressttValuen"); printf(line); printf("t%stt%pt%dn","i", &i, i); printf("t%stt%pt%cn","c", &c, c); printf("t%stt%pt%dn","s", &s, s); printf("t%stt%pt%dn","l", &l, l); printf("t%stt%pt%fn","f", &f, f); printf("t%stt%pt%fn","d", &d, d); printf(line); }
  • 2. Lectures on Numerical Methods 2 Address of a variable This program was compiled by gcc on linux machine. The output of this program: Variable Table ------------------------------- Var Address Value ------------------------------- i 0xbffffc94 0 c 0xbffffc93 a s 0xbffffc90 1 l 0xbffffc8c 2 f 0xbffffc88 3.000000 d 0xbffffc80 4.000000 ------------------------------- #include <stdio.h> #define line "t-----------------n" main() { int i = 0; char c = 'a'; short s = 1; long l = 2; float f = 3.0; double d = 4.0; printf("Variable Tablen"); printf(line); printf("tVarttAddressttValuen"); printf(line); printf("t%stt%pt%dn","i", &i, i); printf("t%stt%pt%cn","c", &c, c); printf("t%stt%pt%dn","s", &s, s); printf("t%stt%pt%dn","l", &l, l); printf("t%stt%pt%fn","f", &f, f); printf("t%stt%pt%fn","d", &d, d); printf(line); }
  • 3. Lectures on Numerical Methods 3 Pointer to a variable If variables have addresses, can these be stored in the variables and manipulated with? These addresses have a new data- types (derived data-types) called pointers. Pointers are declared as data-type *var-name; In this example one pointer variable has been declared as p and it can store addresses of any integer variables. #include <stdio.h> #define line "t----------------n" main() { int i = 0; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %ptt*p = %dn", p, *p); j = *p + 5; printf(“j = %dtt*p = %dn", j, *p); *p = *p + i; printf("p = %ptt*p = %dn", p, *p); p = &f; printf("p = %ptt*p = %dn", p, *p); }
  • 4. Lectures on Numerical Methods 4 Pointer to a variable The simplest operation is to get an address of a variable and store it in a pointer variable. Example p = &k; p = &f; Pointer p is declared to store an address of an int variable. Assigning an address of a float variable to p, would cause a compiler warning or error. (Continuing in spite of warning, may result in disaster). #include <stdio.h> #define line "t----------------n" main() { int i = 0; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %ptt*p = %dn", p, *p); j = *p + 5; printf(“j = %dtt*p = %dn", j, *p); *p = *p + i; printf("p = %ptt*p = %dn", p, *p); p = &f; printf("p = %ptt*p = %dn", p, *p); }
  • 5. Lectures on Numerical Methods 5 Pointer to a variable The value of pointer p is an address of some int variable. The value of the int variable pointed to by p can be accessed by using a dereferencing operator *. Printf statement here prints the value of p and that of *p. p = 0xbffffc8c *p = 2 *p is just like k here. All expressions where k can appear, *p also can. j = 7 *p = 2 #include <stdio.h> #define line "t----------------n" main() { int i = 4; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %ptt*p = %dn", p, *p); j = *p + 5; printf(“j = %dtt*p = %dn", j, *p); *p = *p + i; printf("p = %ptt*p = %dn", p, *p); p = &f; printf("p = %ptt*p = %dn", p, *p); }
  • 6. Lectures on Numerical Methods 6 Pointer to a variable The dereferencing operator *p is not only used to get the value of the variable pointed to by p, but also can be used to change the value of that variable. It is legal to use *p as left side of an assignment. P = 0xbffffc8c *p = 6 #include <stdio.h> #define line "t----------------n" main() { int i = 0; int j = 1; int k = 2; float f = 3.0; int *p = 0; p = &k; printf("p = %ptt*p = %dn", p, *p); j = *p + 5; printf(“j = %dtt*p = %dn", j, *p); *p = *p + i; printf("p = %ptt*p = %dn", p, *p); p = &f; printf("p = %ptt*p = %dn", p, *p); }
  • 7. Lectures on Numerical Methods 7 Pointer Arithmetic A simple arithmetic is allowed for the pointers. A pointer points a variable of a given type. When we add 1 to a pointer variable, it points to the next variable in the memory(though it may not be of the same type). Here p = p + 1 would cause the value of p to be added 4 which is size of int. Look at output. #include <stdio.h> #define line "t----------------n" main() { int i = 0; int j = 1; int k = 2; int *p = 0; p = &k; printf("p = %ptt*p = %dn", p, *p); p = p + 1; printf("p = %ptt*p = %dn", p, *p); p++ ; printf("p = %ptt*p = %dn", p, *p); p = &f; printf("p = %ptt*p = %dn", p, *p); }
  • 8. Lectures on Numerical Methods 8 Pointer Arithmetic Variable Table ---------------------------------------- Var Address Value ---------------------------------------- i 0xbffffc94 0 j 0xbffffc90 1 k 0xbffffc8c 2 f 0xbffffc88 3.000000 p 0xbffffc84 (nil) ---------------------------------------- The output of this program is given here. p = 0xbffffc8c *p = 2 p = 0xbffffc90 *p = 1 p = 0xbffffc94 *p = 0 p = 0xbffffc88 *p = 1077936128
  • 9. Lectures on Numerical Methods 9 Pointers and Functions Arguments  We have seen that since a and b are passed by value to swap1 function, the values of a and b in main are not swapped.  But in swap2, the address of a and address of b is passed. Hence pa and pb contains the addresses of a and b. Then the changes are made directly to the locations of a and b.  The result would be 3 5 5 3 void swap1(int a, int b) { int temp = a; a = b; b = temp; } void swap2(int *pa, int *pb) { int temp = *pa; *pa = *pb; *pb = temp; } main() { int a = 3, b = 5; swap1(a, b); printf(“%dt%d”, a, b); swap2(&a, &b); printf(“%dt%d”, a, b); }
  • 10. Lectures on Numerical Methods 10 Pointers and Functions Arguments  It is indeed inconvenient that a function can return only one value.  Consider a function for the bisection method. The function is expected to return a root of a function. double getRoot(double l, double r, double tol, int maxIter);  What would happen if the root is not bracketed in [l,r] interval? The function is still likely to return some value which is not the required root. Ideally, there should be one more variable indicating the error. double getRoot(double l, double r, double tol, int maxIter, int *error);
  • 11. Lectures on Numerical Methods 11 Pointers and Arrays An array name is a constant pointer. So if a is an array of 10 integers and pa is a pointer to int variable, then Pa = &a[0]; Pa = a; Are legal and same. So are B = a[5]; B = *(a+5); However since a is constant pointer following is invalid A = pa;
  翻译: