SlideShare a Scribd company logo
Gandhinagar institute of technology
subject : CPU(2110003)
Topic Name:
Branch : computer
Batch : CE
Name : Vardhil Patel
Guided By: Prof. __________
Pointer
Pointers
A pointer is a variable that holds a memory
address.
This address is the location of another object in
memory.
For example, if one variable contains the address of
another variable, the first variable is said to point to
the second.
Basically, a pointer contains an address of another
variable.
Pointers provide an indirect means of accessing or
retrieving the data from memory.
Pointers
Variable in
memory
Memory
address
1031000
1001
1002
1003
1004
1005
1006
The ‘&’ and ‘*’
Consider the declaration int ab=3;
This declaration tells the compiler
a) Reserve space in memory to hold the
integer value.
b) Associate the name ab with this memory
location.
c) Store the value 3 at this location.
The ‘&’ and ‘*’
ab
3
Location name
Value at Location
1000 Address
Declaring pointer
Data-type *name;
* is a unary operator, also called as indirection
operator.
Data-type is the type of object which the pointer is
pointing.
Any type of pointer can point to anywhere in the
memory.
* is used to declare a pointer and also to dereference a
pointer.
When you write int *,
compiler assumes that any address that it holds
points to an integer type.
 m= &count;
it means memory address of count variable is stored
into m.
& is unary operator that returns the memory address.
i.e. & (orally called as ampersand) is returning the
address.
 so it means m receives the address of count.
Suppose, count uses memory
Address 2000 to store its value 100.
so, m=&count means m has 2000
address.
q= *m
it returns the value at address m.
value at address 2000 is 100.
so, q will return value 100.
i.e. q receives the value at address m.
count=1002000
 In C every variable must be declared for its data type.
Since pointer variables contain addresses that belong to
a separate data type they must be declared as pointers.
 The declaration of pointer variable takes following form:
this tells the compile there things about the variable
pt_name
1. The asterisk(*) tell that the variable pt_name is a pointer
variable
2. pt_name needs a memory location.
3. pt_name points to a variable of type data_type.
For example:-int *q;
here q is pointer variable that can hold the address
data_type *variable_name;
The ‘&’ and ‘*’
 ‘&’  Address of operator
 ‘*’  Value at address operator. Also called the Indirection
Operator.
 ‘&a’  Returns the address of variable a.
 ‘*a’  Returns the value stored in a particular
address.
An Example:Pointers
main()
{
int j=5;
printf(“Address of j=%dn”,&j);
printf(“Value of j=%dn”,j);
printf(“Value of j=%dn”,*(&j));
}
Output:
Address of j = 1000
Value of j = 5
Value of j = 5
Example: p=&a
p=7
a=3
b=5
65520
65523
65525
%d,a = 3
%d,b = 5
%u,&a = 65523
%u,&b = 65525
%u,p = 65523
%u,&p = 65520
%u,*p = 3
Initializing Pointer
 Address of some variable can be assigned to a pointer
variable at the time of declaration of the pointer
variable.
 For ex:
int num; int num;
int *ptr=# int *p;
p=#
 These two statements above are equivalent to
following statements.
4/25/2017 Ashim Lamichhane 13
1/14/10
NULL
• NULL is a special value which may be assigned to a pointer
• NULL indicates that this pointer does not point to any variable
(there is no pointee)
• Often used when pointers are declared
int *pInt = NULL;
• Often used as the return type of functions that return a pointer to
indicate function failure
int *myPtr;
myPtr = myFunction( );
if (myPtr == NULL){
/* something bad happened */
}
• Dereferencing a pointer whose value is NULL will result in
program termination.
15
Pointer Expressions
Expressions involving pointers can be classified as
follows:
 Pointer Assignments
 Pointer Arithmetic
 Pointer Comparison
 Pointer Conversion
16
Pointer Assignments
Consider the following example
i
5
1000
1000
2000
j
Here i’s value is 5 and j’s value is i’s address.
17
Pointer Assignments
Since, the variable j is containing an address, it is
declared as int *j;
This declaration tells the compiler that j will be used to
store the address of an integer value – i.e j points to an
integer.
18
An Example:Pointer Assignments
main()
{
int j=3;
int *k;
k = &j;
printf(“Address of j=%d”,&j);
printf(“Address of j=%d”,k);
printf(“Address of j=%d”,&k);
printf(“Value of k=%d”,k);
printf(“Value of j=%d”,j);
printf(“Value of j=%d”,*(&j));
printf(“Value of j=%d”,*j);
}
Output:
Address of j = 1000
Address of j = 1000
Address of k = 2000
Value of k = 1000
Value of j = 3
Value of j = 3
Value of j = error
Pointer Arithmetic There are only two arithmetic operations that can be
used on pointers
 Addition
 Subtraction
 To understand this concept, lets p1 be an integer
pointer with value 2000 address.
 int is of 2 bytes
 After expression p1++;
 P1 contains address 2002 not 2001.
 Each time p1 is incremented, it will point to next
integer.
 The same is true for decrement.
 for p1--;
 Causes value of p1 to be 1998.
 Each time a pointer is incremented, it points to the
memory location of the next element of its base type.
 If decremented, then it points to previous element
location.
 P1=p1+12; makes p1 points to 12th element of p1 type.
Arithmetic Rules
 You cannot multiply or divide pointers.
 You cannot add or subtract two pointers.
 You cannot apply bitwise operators to them.
 You cannot add or subtract type float or double to or
from pointers.
Pointer Comparison
 You can compare two pointers in a relational
expression, example:
if(p<q)
printf(“p points to lower memory than q n”);
 Pointer comparison are useful only when two pointers
point to a common object such as an array.
23
Pointer Conversions
One type of pointer can be converted to another type of pointer.
Consider the following example:
main()
{
double x = 100.1,y;
int *p;
/*The next statement causes p to point to double*/
p = (int*)&x;
y = *p;
printf(“The value of x is: %f”,y);
}
POINTER TO POINTER (Double Pointer)
 C allows the use of pointers that point to other
pointers and these in turn, point to data.
 For pointers to do that, we only need to add asterisk
(*) for each level of reference. For example:
int a=20;
int *p;
int **q;  pointer to “a pointer to
an integer”
p=&a;
q=&p;
• To refer to variable ‘a’ using pointer
‘q’, dereference it once i.e. *p
• To refer to variable ‘a’ using pointer
‘q’, dereference it twice because
there are two levels of indirection
involved.
• Both *p and **q displays 20 if they
are printed with a printf statement.
Double Pointer
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var;
/* take the address of ptr using
address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %dn", var );
printf("Value available at *ptr =
%dn", *ptr );
printf("Value available at **pptr =
%dn", **pptr);
return 0;
}
OUTPUT
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
Array Of Pointers
 An array of pointers can be declared as
data_type *pointer_name[size];
 For ex:
int *p[10];
 This declares an array of 10 pointers, each of which points to an integer.
The first pointer is called p[0], the second is p[1] and so on up to p[9].
 Initially, these pointers are uninitialized and they can be used as below.
int a=10, b=100, c=1000;
p[0]=&a;
p[1]=&b;
p[2]=&c; and so on.
Relationship between 1-D array and pointer
 Array name by itself is an address or pointer.
 It points to the address of the first element(0th element
of an array)
 If x is 1D array, the address of the first element can be
expressed as &x[0] or as x.
 Similarly address of second array element can be
written as &x[1]or x+1.
 In general, address on an array element i can be
expressed as &x[i] or x+i
• In general address of array element i can be
expressed as &x[i] or x+i
• x[i] and *(x+i) both represents represents the
content of the address.
Array x
Address of
first
array
element
can be
expressed as
&x[0] or
simply x
x[0] x[1] x[2] x[3]
Array x
Address of
second
array
element
can be
expressed as
&x[1] or
simply x+1
x[0] x[1] x[2] x[3]
#include <stdio.h>
int main(){
int x[5]={20,40,60,80,100},k;
printf("narray element ttelements value
ttaddressn");
for(k=0;k<5;k++){
printf("x[%d]ttt%dttt%pn",k,*(x+k),x+k );
}
}
array
element
elements
value
address
x[0] 20 0x7fff5bb0
bbb0
x[1] 40 0x7fff5bb0
bbb4
x[2] 60 0x7fff5bb0
bbb8
x[3] 80 0x7fff5bb0
bbbc
x[4] 100 0x7fff5bb0
bbc0
To display array element with their address using array name as a pointer
OUTPUT
• Element k acts as the element number( 0,1,2,3,4)
• x acting array is added with k i.e k is added with the address of first
element, it points to the consecutive memory location.
• Thus &x[k] is same as *(x+k)
/* WAP to calculate average marks of 10 students in a subject
using pointer*/
#include <stdio.h>
int main(void){
float marks[10],sum=0;
int i;
float avg;
printf("Enter marks of 10 students: ");
for(i=0;i<10;i++){
scanf("%f",marks+i);
sum+=*(marks+i);
}
avg=sum/10;
printf("nThe average is=%fn", avg);
}
4/25/2017 Ashim Lamichhane 31
mark
s+0
mark
s+1
mark
s+2
mark
s+3
mark
s+4
Pointers and 2-D Arrays
 A two dimensional array is actually a collection of one
dimensional arrays, each indicating a row (i.e. 2-D array can be
thought as one dimensional array of rows).
 It is stored in memory in the row form. For ex.
1 2 3
4 5 6
7 8 9
• Is stored in the row major order in memory as illustrated
below
1 2 3 4 5 6 7 8 9
a[0][0] a[0][1] a[0][2] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2]
65500 65502 65504 65506 65508 65510 65512 65514 65516
a=
Syntax for declaration of 2-D array
 data_type (*ptr_var)[size2];
 Instead of data_type array[size1][size2];
 Ex: Suppose x is a two dimensional integer array having 4 rows
and 5 columns. We declare x as
int (*x)[5];
rather than
int x[4][5];
 x points to the first 5 element array, which is actually first row of
the two dimensional array.
 Similarly x+1 points to the second 5 element array, which is the
second row of the two dimensional array
Passing Pointer to a Function
 A pointer can be passed to a function as an argument.
 Passing a pointer means passing address of a variable
instead of value of the variable.
 As address is passed in this case, this mechanism is also
known as call by address or call by reference.
 As address of variable is passed in this mechanism, if
value in the passed address is changed within function
definition, the value of actual variable is also changed.
#include <stdio.h>
void conversion(char *);
int main(){
char input;
printf("Enter character of your
choice: ");
scanf("%c",&input);
conversion(&input);
printf("The corresponding
character is: %cn",input );
}
void conversion(char *c){
if (*c>=97 && *c<=122)
{
*c=*c-32;
}
else if(*c >=65 && *c<=90){
*c=*c+32;
}
}
Output
Enter Character of Your
Choice: a
The corresponding Char is:
A
Enter Character of Your
Choice: b
The corresponding Char is:
B
String And Pointer
 As strings are arrays and arrays are closely connected with
pointers, we can say that string and pointers are closely
related.
char name[5]=“shyam”;
 As the string variable name is an array of characters, it is a
pointer to the first character of the string and can be used
to access and manipulate the characters of the string.
 When a pointer to char is printed in the format of a string, it
will start to print the pointer character and then successive
characters until the end of string is reached.
 Thus name prints “shyam”, name+1 prints “hyam”, name+2
prints “yam” and so on.
Pointers

More Related Content

What's hot

10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer웅식 전
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
Faisal Shahzad Khan
 
C pointers
C pointersC pointers
C pointers
Aravind Mohan
 
C Pointers
C PointersC Pointers
C Pointers
omukhtar
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
madan reddy
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Kamal Acharya
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
Abdullah Jan
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
Dr-Dipali Meher
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
Ali Aminian
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
gourav kottawar
 
Pointer in C
Pointer in CPointer in C
Pointer in C
Sonya Akter Rupa
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
aarockiaabinsAPIICSE
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Prabhu Govind
 
This pointer
This pointerThis pointer
This pointer
Kamal Acharya
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in C
Tushar B Kute
 

What's hot (20)

10. array & pointer
10. array & pointer10. array & pointer
10. array & pointer
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Pointer
PointerPointer
Pointer
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
C pointers
C pointersC pointers
C pointers
 
C Pointers
C PointersC Pointers
C Pointers
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
detailed information about Pointers in c language
detailed information about Pointers in c languagedetailed information about Pointers in c language
detailed information about Pointers in c language
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
This pointer
This pointerThis pointer
This pointer
 
Module 02 Pointers in C
Module 02 Pointers in CModule 02 Pointers in C
Module 02 Pointers in C
 

Similar to Pointers

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.pptx
pointers.pptxpointers.pptx
pointers.pptx
janithlakshan1
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
s170883BesiVyshnavi
 
Ch 7-pointers
Ch 7-pointersCh 7-pointers
Ch 7-pointers
Muslimee Subhany
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
Krishna Nanda
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
chapter-11-pointers.pdf
chapter-11-pointers.pdfchapter-11-pointers.pdf
chapter-11-pointers.pdf
study material
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
sudhakargeruganti
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
Vikram Nandini
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
ShivamChaturvedi67
 
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
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi96
 
Pointers
PointersPointers
Pointers
Prasadu Peddi
 
Ponters
PontersPonters
Ponters
Anil Dutt
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
minal kumar soni
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
SIMONTHOMAS S
 

Similar to Pointers (20)

Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Ch 7-pointers
Ch 7-pointersCh 7-pointers
Ch 7-pointers
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
chapter-11-pointers.pdf
chapter-11-pointers.pdfchapter-11-pointers.pdf
chapter-11-pointers.pdf
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
l7-pointers.ppt
l7-pointers.pptl7-pointers.ppt
l7-pointers.ppt
 
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
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Pointers
PointersPointers
Pointers
 
Ponters
PontersPonters
Ponters
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 

Recently uploaded

WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
MuhammadTufail242431
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
Kamal Acharya
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
seandesed
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 

Recently uploaded (20)

WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Vaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdfVaccine management system project report documentation..pdf
Vaccine management system project report documentation..pdf
 
Architectural Portfolio Sean Lockwood
Architectural Portfolio Sean LockwoodArchitectural Portfolio Sean Lockwood
Architectural Portfolio Sean Lockwood
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 

Pointers

  • 1. Gandhinagar institute of technology subject : CPU(2110003) Topic Name: Branch : computer Batch : CE Name : Vardhil Patel Guided By: Prof. __________ Pointer
  • 2. Pointers A pointer is a variable that holds a memory address. This address is the location of another object in memory. For example, if one variable contains the address of another variable, the first variable is said to point to the second. Basically, a pointer contains an address of another variable. Pointers provide an indirect means of accessing or retrieving the data from memory.
  • 4. The ‘&’ and ‘*’ Consider the declaration int ab=3; This declaration tells the compiler a) Reserve space in memory to hold the integer value. b) Associate the name ab with this memory location. c) Store the value 3 at this location.
  • 5. The ‘&’ and ‘*’ ab 3 Location name Value at Location 1000 Address
  • 6. Declaring pointer Data-type *name; * is a unary operator, also called as indirection operator. Data-type is the type of object which the pointer is pointing. Any type of pointer can point to anywhere in the memory. * is used to declare a pointer and also to dereference a pointer.
  • 7. When you write int *, compiler assumes that any address that it holds points to an integer type.  m= &count; it means memory address of count variable is stored into m. & is unary operator that returns the memory address. i.e. & (orally called as ampersand) is returning the address.  so it means m receives the address of count.
  • 8. Suppose, count uses memory Address 2000 to store its value 100. so, m=&count means m has 2000 address. q= *m it returns the value at address m. value at address 2000 is 100. so, q will return value 100. i.e. q receives the value at address m. count=1002000
  • 9.  In C every variable must be declared for its data type. Since pointer variables contain addresses that belong to a separate data type they must be declared as pointers.  The declaration of pointer variable takes following form: this tells the compile there things about the variable pt_name 1. The asterisk(*) tell that the variable pt_name is a pointer variable 2. pt_name needs a memory location. 3. pt_name points to a variable of type data_type. For example:-int *q; here q is pointer variable that can hold the address data_type *variable_name;
  • 10. The ‘&’ and ‘*’  ‘&’  Address of operator  ‘*’  Value at address operator. Also called the Indirection Operator.  ‘&a’  Returns the address of variable a.  ‘*a’  Returns the value stored in a particular address.
  • 11. An Example:Pointers main() { int j=5; printf(“Address of j=%dn”,&j); printf(“Value of j=%dn”,j); printf(“Value of j=%dn”,*(&j)); } Output: Address of j = 1000 Value of j = 5 Value of j = 5
  • 12. Example: p=&a p=7 a=3 b=5 65520 65523 65525 %d,a = 3 %d,b = 5 %u,&a = 65523 %u,&b = 65525 %u,p = 65523 %u,&p = 65520 %u,*p = 3
  • 13. Initializing Pointer  Address of some variable can be assigned to a pointer variable at the time of declaration of the pointer variable.  For ex: int num; int num; int *ptr=&num; int *p; p=&num;  These two statements above are equivalent to following statements. 4/25/2017 Ashim Lamichhane 13
  • 14. 1/14/10 NULL • NULL is a special value which may be assigned to a pointer • NULL indicates that this pointer does not point to any variable (there is no pointee) • Often used when pointers are declared int *pInt = NULL; • Often used as the return type of functions that return a pointer to indicate function failure int *myPtr; myPtr = myFunction( ); if (myPtr == NULL){ /* something bad happened */ } • Dereferencing a pointer whose value is NULL will result in program termination.
  • 15. 15 Pointer Expressions Expressions involving pointers can be classified as follows:  Pointer Assignments  Pointer Arithmetic  Pointer Comparison  Pointer Conversion
  • 16. 16 Pointer Assignments Consider the following example i 5 1000 1000 2000 j Here i’s value is 5 and j’s value is i’s address.
  • 17. 17 Pointer Assignments Since, the variable j is containing an address, it is declared as int *j; This declaration tells the compiler that j will be used to store the address of an integer value – i.e j points to an integer.
  • 18. 18 An Example:Pointer Assignments main() { int j=3; int *k; k = &j; printf(“Address of j=%d”,&j); printf(“Address of j=%d”,k); printf(“Address of j=%d”,&k); printf(“Value of k=%d”,k); printf(“Value of j=%d”,j); printf(“Value of j=%d”,*(&j)); printf(“Value of j=%d”,*j); } Output: Address of j = 1000 Address of j = 1000 Address of k = 2000 Value of k = 1000 Value of j = 3 Value of j = 3 Value of j = error
  • 19. Pointer Arithmetic There are only two arithmetic operations that can be used on pointers  Addition  Subtraction  To understand this concept, lets p1 be an integer pointer with value 2000 address.  int is of 2 bytes  After expression p1++;  P1 contains address 2002 not 2001.
  • 20.  Each time p1 is incremented, it will point to next integer.  The same is true for decrement.  for p1--;  Causes value of p1 to be 1998.  Each time a pointer is incremented, it points to the memory location of the next element of its base type.  If decremented, then it points to previous element location.  P1=p1+12; makes p1 points to 12th element of p1 type.
  • 21. Arithmetic Rules  You cannot multiply or divide pointers.  You cannot add or subtract two pointers.  You cannot apply bitwise operators to them.  You cannot add or subtract type float or double to or from pointers.
  • 22. Pointer Comparison  You can compare two pointers in a relational expression, example: if(p<q) printf(“p points to lower memory than q n”);  Pointer comparison are useful only when two pointers point to a common object such as an array.
  • 23. 23 Pointer Conversions One type of pointer can be converted to another type of pointer. Consider the following example: main() { double x = 100.1,y; int *p; /*The next statement causes p to point to double*/ p = (int*)&x; y = *p; printf(“The value of x is: %f”,y); }
  • 24. POINTER TO POINTER (Double Pointer)  C allows the use of pointers that point to other pointers and these in turn, point to data.  For pointers to do that, we only need to add asterisk (*) for each level of reference. For example: int a=20; int *p; int **q;  pointer to “a pointer to an integer” p=&a; q=&p; • To refer to variable ‘a’ using pointer ‘q’, dereference it once i.e. *p • To refer to variable ‘a’ using pointer ‘q’, dereference it twice because there are two levels of indirection involved. • Both *p and **q displays 20 if they are printed with a printf statement.
  • 25.
  • 26. Double Pointer int main () { int var; int *ptr; int **pptr; var = 3000; ptr = &var; /* take the address of ptr using address of operator & */ pptr = &ptr; /* take the value using pptr */ printf("Value of var = %dn", var ); printf("Value available at *ptr = %dn", *ptr ); printf("Value available at **pptr = %dn", **pptr); return 0; } OUTPUT Value of var = 3000 Value available at *ptr = 3000 Value available at **pptr = 3000
  • 27. Array Of Pointers  An array of pointers can be declared as data_type *pointer_name[size];  For ex: int *p[10];  This declares an array of 10 pointers, each of which points to an integer. The first pointer is called p[0], the second is p[1] and so on up to p[9].  Initially, these pointers are uninitialized and they can be used as below. int a=10, b=100, c=1000; p[0]=&a; p[1]=&b; p[2]=&c; and so on.
  • 28. Relationship between 1-D array and pointer  Array name by itself is an address or pointer.  It points to the address of the first element(0th element of an array)  If x is 1D array, the address of the first element can be expressed as &x[0] or as x.  Similarly address of second array element can be written as &x[1]or x+1.  In general, address on an array element i can be expressed as &x[i] or x+i
  • 29. • In general address of array element i can be expressed as &x[i] or x+i • x[i] and *(x+i) both represents represents the content of the address. Array x Address of first array element can be expressed as &x[0] or simply x x[0] x[1] x[2] x[3] Array x Address of second array element can be expressed as &x[1] or simply x+1 x[0] x[1] x[2] x[3]
  • 30. #include <stdio.h> int main(){ int x[5]={20,40,60,80,100},k; printf("narray element ttelements value ttaddressn"); for(k=0;k<5;k++){ printf("x[%d]ttt%dttt%pn",k,*(x+k),x+k ); } } array element elements value address x[0] 20 0x7fff5bb0 bbb0 x[1] 40 0x7fff5bb0 bbb4 x[2] 60 0x7fff5bb0 bbb8 x[3] 80 0x7fff5bb0 bbbc x[4] 100 0x7fff5bb0 bbc0 To display array element with their address using array name as a pointer OUTPUT • Element k acts as the element number( 0,1,2,3,4) • x acting array is added with k i.e k is added with the address of first element, it points to the consecutive memory location. • Thus &x[k] is same as *(x+k)
  • 31. /* WAP to calculate average marks of 10 students in a subject using pointer*/ #include <stdio.h> int main(void){ float marks[10],sum=0; int i; float avg; printf("Enter marks of 10 students: "); for(i=0;i<10;i++){ scanf("%f",marks+i); sum+=*(marks+i); } avg=sum/10; printf("nThe average is=%fn", avg); } 4/25/2017 Ashim Lamichhane 31 mark s+0 mark s+1 mark s+2 mark s+3 mark s+4
  • 32. Pointers and 2-D Arrays  A two dimensional array is actually a collection of one dimensional arrays, each indicating a row (i.e. 2-D array can be thought as one dimensional array of rows).  It is stored in memory in the row form. For ex. 1 2 3 4 5 6 7 8 9 • Is stored in the row major order in memory as illustrated below 1 2 3 4 5 6 7 8 9 a[0][0] a[0][1] a[0][2] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2] 65500 65502 65504 65506 65508 65510 65512 65514 65516 a=
  • 33. Syntax for declaration of 2-D array  data_type (*ptr_var)[size2];  Instead of data_type array[size1][size2];  Ex: Suppose x is a two dimensional integer array having 4 rows and 5 columns. We declare x as int (*x)[5]; rather than int x[4][5];  x points to the first 5 element array, which is actually first row of the two dimensional array.  Similarly x+1 points to the second 5 element array, which is the second row of the two dimensional array
  • 34. Passing Pointer to a Function  A pointer can be passed to a function as an argument.  Passing a pointer means passing address of a variable instead of value of the variable.  As address is passed in this case, this mechanism is also known as call by address or call by reference.  As address of variable is passed in this mechanism, if value in the passed address is changed within function definition, the value of actual variable is also changed.
  • 35. #include <stdio.h> void conversion(char *); int main(){ char input; printf("Enter character of your choice: "); scanf("%c",&input); conversion(&input); printf("The corresponding character is: %cn",input ); } void conversion(char *c){ if (*c>=97 && *c<=122) { *c=*c-32; } else if(*c >=65 && *c<=90){ *c=*c+32; } } Output Enter Character of Your Choice: a The corresponding Char is: A Enter Character of Your Choice: b The corresponding Char is: B
  • 36. String And Pointer  As strings are arrays and arrays are closely connected with pointers, we can say that string and pointers are closely related. char name[5]=“shyam”;  As the string variable name is an array of characters, it is a pointer to the first character of the string and can be used to access and manipulate the characters of the string.  When a pointer to char is printed in the format of a string, it will start to print the pointer character and then successive characters until the end of string is reached.  Thus name prints “shyam”, name+1 prints “hyam”, name+2 prints “yam” and so on.