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;

More Related Content

Similar to iit c prog.ppt

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
 
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
 
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
 
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
PontersPonters
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
SIMONTHOMAS S
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
georgejustymirobi1
 
c program.ppt
c program.pptc program.ppt
c program.ppt
mouneeshwarans
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
AmIt Prasad
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
Srikanth
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
ManjeeraBhargavi Varanasi
 

Similar to iit c prog.ppt (20)

Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
Ch 7-pointers
Ch 7-pointersCh 7-pointers
Ch 7-pointers
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Pointers
PointersPointers
Pointers
 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Pointers
PointersPointers
Pointers
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Ponters
PontersPonters
Ponters
 
Ponters
PontersPonters
Ponters
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
presentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).pptpresentation_pointers_1444076066_140676 (1).ppt
presentation_pointers_1444076066_140676 (1).ppt
 
c program.ppt
c program.pptc program.ppt
c program.ppt
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
 

Recently uploaded

math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 

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;