SlideShare a Scribd company logo
Pointers in C
Definition
 A Pointer is a variable that holds
address of another variable of same
data type.
 also known as locator or indicator that
points to an address of a value.
Benefits of using pointers
 Pointer reduces the
code and improves the performance, it
is used to retrieving strings, trees etc.
and used with arrays, structures and
functions.
 We can return multiple values from
function using pointer.
 It makes you able to access any
memory location in the computer's
memory.
 It allows C to support dynamic memory
Concept of pointer
 Whenever a variable is declared in the
program, system allocates a location i.e a
name to that variable in the memory, to hold
the assigned value. This location has its
own address number.
 Let us assume that system has allocated
memory location 80F for a variable a.
int a = 10 ;
 We can access the value 10 by either using the
variable name a or the address 80F. Since the
memory addresses are simply numbers they
can be assigned to some other variable. The
variable that holds memory address are
called pointer variables. A pointer variable is
therefore nothing but a variable that contains
an address, which is a location of another
variable. Value of pointer variable will be
stored in another memory location.
Declaring a pointer variable
 General syntax of pointer declaration
is,
 Data type of a pointer must be same
as the data type of a variable to which
the pointer variable is
pointing. void type pointer works with
all data types, but is not used often
used.
Initialization of Pointer
variable
 Pointer Initialization is the process of
assigning address of a variable
to pointer variable. Pointer variable
contains address of variable of same
data type.
 In C language address operator & is
used to determine the address of a
variable.
 The & (immediately preceding a variable
name) returns the address of the
variable associated with it.
Pointer variable always points to same type of
data.
Note: If you do not have an exact address to be
assigned to a pointer variable while declaration, It
is recommended to assign a NULL value to the
pointer variable. A pointer which is assigned a
NULL value is called a null pointer.
Dereferencing of Pointer
 Once a pointer has been assigned the
address of a variable. To access the
value of variable, pointer is
dereferenced, using the indirection
operator *.
Points to remember:
 While declaring/initializing the pointer
variable, * indicates that the variable is a
pointer.
 The address of any variable is given by
preceding the variable name with
Ampersand '&'.
 The pointer variable stores the address of a
variable. The declaration int *a doesn't mean
that a is going to contain an integer value. It
means that a is going to contain the address
of a variable storing integer value.
 To access the value of a certain address
stored by a pointer variable, * is used. Here,
the * can be read as 'value at'.
A Simple program to explain
pointers:
Pointer Arithmetic
 16 bit Machine ( Turbo C )
In a 16 bit machine, size of all types of
pointer, be
it int*, float*, char* or double* is
always 2 bytes.
But when we perform any arithmetic
function like increment on a pointer,
changes occur as per the size of their
primitive data type.
Pointers in c language
Examples for Pointer
Arithmetic
In the above case, pointer will be of 2 bytes. And when we increment
it, it will increment by 2 bytes because int is also of 2 bytes.
In this case, size of pointer is still 2 bytes. But now, when we
increment it, it will increment by 4 bytes because float is of 4 bytes.
Similarly, in this case, size of pointer is still 2 bytes. But now, when
we increment it, it will increment by 8 bytes because its data type
is double.
32 bit Machine (Visual Basic
C++)
 The concept of pointer arithmetic
remains exact same, but the size of
pointer and various datatypes is
different in a 32 bit machine. Pointer
in 32 bit machine is of 4 bytes.
 And, following is a table for Size of
datatypes on 32-bit Machine :
 Note: We cannot add two pointers. This is
because pointers contain addresses, adding
two addresses makes no sense, because you
have no idea what it would point to.But we
can subtract two pointers.This is because
difference between two pointers gives the
number of elements of its data type that can
be stored between the two pointers.
Program for pointer
arithmetic(32-bit machine)
 Explanation of the above program:
 Point 1: Here, * means 'value at the given
address'. Thus, it adds the value of m and n
which is 15.
 Point 2: It subtracts the addresses of the two
variables and then divides it by the size of the
pointer datatype (here integer, which has size
of 4 bytes) which gives us the number of
elements of integer data type that can be
stored within it.
 Point 3: It increments the address stored by
the pointer by the size of its datatype(here 4).
 Point 4: It decrements the address stored by
the pointer by the size of its datatype(here 4).
 Point 5: Addition of two pointers is not allowed.
Pointer and Arrays
 When an array is declared, compiler
allocates sufficient amount of memory
to contain all the elements of the
array. Base address i.e address of the
first element of the array is also
allocated by the compiler.
 Suppose we declare an array arr,
 Assuming that the base address of arr is 1000
and each integer requires two bytes, the five
elements will be stored as follows:
 Here variable arr will give the base address,
which is a constant pointer pointing to the
element, arr[0].
 Therefore arr is containing the address
of arr[0] i.e 1000.
 In short, arr has two purpose - it is the name of
an array and it acts as a pointer pointing towards
the first element in the array.
arr is equal to &arr[0] //by default
 We can declare a pointer of type int to
point to the array arr.
 Now we can access every element of
array arr using p++ to move from one
element to another.
NOTE : You cannot decrement a pointer
once incremented. p-- won't work.
Example
void main()
{
int x[5]={2,4,6,8,10},i=0;
clrscr();
printf(“nElement Number Element
Address”);
while(i<5)
{
printf(“nx[%d] = t%d
%u”,i,*(x+i),x+i);
i++;
}
Output
Element Number Element Address
X[0]= 2 4056
X[1]= 4 4058
X[2]= 6 4060
X[3]= 8 4062
X[4]= 10 4064
Pointer to Array
 we can use a pointer to point to an Array,
and then we can use that pointer to
access the array.
 Example:
 In the above program, the pointer *p will
print all the values stored in the array
one by one. We can also use the Base
address (a in above case) to act as
pointer and print all the values.
Pointers in c language
 The generalized form for using pointer
with an array,
is same as:
Array of pointers
Example, which uses an array of 3
integers −
When the above code is compiled and
executed, it produces the following
result −
 There may be a situation when we
want to maintain an array, which can
store pointers to an int or char or any
other data type available. Following is
the declaration of an array of pointers
to an integer −
 It declares ptr as an array of MAX
integer pointers. Thus, each element
in ptr, holds a pointer to an int value.
 The following example uses three integers,
which are stored in an array of pointers, as
follows −
When the above code is compiled and
executed, it produces the following result −
Pointers to Function
 It is possible to declare a pointer
pointing to a function which can then
be used as an argument in another
function. A pointer to a function is
declared as follows,
 A function pointer can point to a
specific function when it is assigned
the name of the function.
 s is a pointer to a function sum.
Now sum can be called using function
pointer s with the list of parameter.
Example of Pointer to
Function

More Related Content

What's hot (20)

PPTX
Data types in C language
kashyap399
 
PPTX
Union in c language
tanmaymodi4
 
PPTX
Basic Data Types in C++
Hridoy Bepari
 
PPTX
Type casting in c programming
Rumman Ansari
 
PPT
File handling in c
David Livingston J
 
PPTX
Pointer arithmetic in c
sangrampatil81
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPTX
Structure in C
Kamal Acharya
 
PPTX
python conditional statement.pptx
Dolchandra
 
PPT
Basics of C programming
avikdhupar
 
PPTX
File in C language
Manash Kumar Mondal
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PPTX
Storage class in C Language
Nitesh Kumar Pandey
 
PPTX
Inheritance in c++
Vishal Patil
 
PPTX
Dynamic memory allocation in c
lavanya marichamy
 
PPTX
Pointer in c
Imamul Kadir
 
PPT
Pointers C programming
Appili Vamsi Krishna
 
PPTX
Structures in c language
tanmaymodi4
 
PPTX
Computer registers
DeepikaT13
 
Data types in C language
kashyap399
 
Union in c language
tanmaymodi4
 
Basic Data Types in C++
Hridoy Bepari
 
Type casting in c programming
Rumman Ansari
 
File handling in c
David Livingston J
 
Pointer arithmetic in c
sangrampatil81
 
Programming in c Arrays
janani thirupathi
 
Structure in C
Kamal Acharya
 
python conditional statement.pptx
Dolchandra
 
Basics of C programming
avikdhupar
 
File in C language
Manash Kumar Mondal
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Storage class in C Language
Nitesh Kumar Pandey
 
Inheritance in c++
Vishal Patil
 
Dynamic memory allocation in c
lavanya marichamy
 
Pointer in c
Imamul Kadir
 
Pointers C programming
Appili Vamsi Krishna
 
Structures in c language
tanmaymodi4
 
Computer registers
DeepikaT13
 

Similar to Pointers in c language (20)

PPTX
C pointer
University of Potsdam
 
PDF
Pointers
Prasadu Peddi
 
PPT
Lect 9(pointers) Zaheer Abbas
Information Technology Center
 
PPT
Lect 8(pointers) Zaheer Abbas
Information Technology Center
 
PDF
Pointers in c++
Asaye Dilbo
 
DOCX
PPS 9.9.POINTERS IDEA OF POINTERS, DEFINING POINTERS, USE OF POINTERS IN SEL...
Sitamarhi Institute of Technology
 
PDF
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PPTX
Arrays to arrays and pointers with arrays.pptx
Ramakrishna Reddy Bijjam
 
PDF
C++ Pointers , Basic to advanced Concept
M.H.Saboo Siddik Polytechnic
 
PPT
13092119343434343432232323121211213435554
simplyamrita2011
 
PPTX
c++ arrays and pointers grade 9 STEP curriculum.pptx
JanineCallangan
 
PPTX
pointers.pptx
janithlakshan1
 
PPTX
Pointers in c
CHANDAN KUMAR
 
PDF
PROBLEM SOLVING USING PPSC- UNIT -4.pdf
JNTUK KAKINADA
 
PDF
Pointers
Swarup Boro
 
PPTX
pointer.pptx module of information technology
jolynetomas
 
PDF
Pointers in C
Monishkanungo
 
PPTX
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
PPTX
Pointers in c++
sai tarlekar
 
PDF
Pointer in c++ part1
Subhasis Nayak
 
Pointers
Prasadu Peddi
 
Lect 9(pointers) Zaheer Abbas
Information Technology Center
 
Lect 8(pointers) Zaheer Abbas
Information Technology Center
 
Pointers in c++
Asaye Dilbo
 
PPS 9.9.POINTERS IDEA OF POINTERS, DEFINING POINTERS, USE OF POINTERS IN SEL...
Sitamarhi Institute of Technology
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Arrays to arrays and pointers with arrays.pptx
Ramakrishna Reddy Bijjam
 
C++ Pointers , Basic to advanced Concept
M.H.Saboo Siddik Polytechnic
 
13092119343434343432232323121211213435554
simplyamrita2011
 
c++ arrays and pointers grade 9 STEP curriculum.pptx
JanineCallangan
 
pointers.pptx
janithlakshan1
 
Pointers in c
CHANDAN KUMAR
 
PROBLEM SOLVING USING PPSC- UNIT -4.pdf
JNTUK KAKINADA
 
Pointers
Swarup Boro
 
pointer.pptx module of information technology
jolynetomas
 
Pointers in C
Monishkanungo
 
C++ - UNIT_-_IV.pptx which contains details about Pointers
ANUSUYA S
 
Pointers in c++
sai tarlekar
 
Pointer in c++ part1
Subhasis Nayak
 
Ad

More from Tanmay Modi (12)

PPTX
Preprocessor directives in c laguage
Tanmay Modi
 
PPTX
Loops in c language
Tanmay Modi
 
PPTX
Generations of computers
Tanmay Modi
 
PPTX
Storage classes in c language
Tanmay Modi
 
PPTX
Union in c language
Tanmay Modi
 
PPTX
Structures in c language
Tanmay Modi
 
PPTX
Operators inc c language
Tanmay Modi
 
PPTX
Functions in c language
Tanmay Modi
 
PPTX
Dynamic memory allocation in c language
Tanmay Modi
 
PPTX
Decision statements in c laguage
Tanmay Modi
 
PPTX
Arrays in c v1 09102017
Tanmay Modi
 
PPTX
Cryptocurrency
Tanmay Modi
 
Preprocessor directives in c laguage
Tanmay Modi
 
Loops in c language
Tanmay Modi
 
Generations of computers
Tanmay Modi
 
Storage classes in c language
Tanmay Modi
 
Union in c language
Tanmay Modi
 
Structures in c language
Tanmay Modi
 
Operators inc c language
Tanmay Modi
 
Functions in c language
Tanmay Modi
 
Dynamic memory allocation in c language
Tanmay Modi
 
Decision statements in c laguage
Tanmay Modi
 
Arrays in c v1 09102017
Tanmay Modi
 
Cryptocurrency
Tanmay Modi
 
Ad

Recently uploaded (20)

PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Dimensions of Societal Planning in Commonism
StefanMz
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Horarios de distribución de agua en julio
pegazohn1978
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 

Pointers in c language

  • 2. Definition  A Pointer is a variable that holds address of another variable of same data type.  also known as locator or indicator that points to an address of a value.
  • 3. Benefits of using pointers  Pointer reduces the code and improves the performance, it is used to retrieving strings, trees etc. and used with arrays, structures and functions.  We can return multiple values from function using pointer.  It makes you able to access any memory location in the computer's memory.  It allows C to support dynamic memory
  • 4. Concept of pointer  Whenever a variable is declared in the program, system allocates a location i.e a name to that variable in the memory, to hold the assigned value. This location has its own address number.  Let us assume that system has allocated memory location 80F for a variable a. int a = 10 ;
  • 5.  We can access the value 10 by either using the variable name a or the address 80F. Since the memory addresses are simply numbers they can be assigned to some other variable. The variable that holds memory address are called pointer variables. A pointer variable is therefore nothing but a variable that contains an address, which is a location of another variable. Value of pointer variable will be stored in another memory location.
  • 6. Declaring a pointer variable  General syntax of pointer declaration is,  Data type of a pointer must be same as the data type of a variable to which the pointer variable is pointing. void type pointer works with all data types, but is not used often used.
  • 7. Initialization of Pointer variable  Pointer Initialization is the process of assigning address of a variable to pointer variable. Pointer variable contains address of variable of same data type.  In C language address operator & is used to determine the address of a variable.  The & (immediately preceding a variable name) returns the address of the variable associated with it.
  • 8. Pointer variable always points to same type of data. Note: If you do not have an exact address to be assigned to a pointer variable while declaration, It is recommended to assign a NULL value to the pointer variable. A pointer which is assigned a NULL value is called a null pointer.
  • 9. Dereferencing of Pointer  Once a pointer has been assigned the address of a variable. To access the value of variable, pointer is dereferenced, using the indirection operator *.
  • 10. Points to remember:  While declaring/initializing the pointer variable, * indicates that the variable is a pointer.  The address of any variable is given by preceding the variable name with Ampersand '&'.  The pointer variable stores the address of a variable. The declaration int *a doesn't mean that a is going to contain an integer value. It means that a is going to contain the address of a variable storing integer value.  To access the value of a certain address stored by a pointer variable, * is used. Here, the * can be read as 'value at'.
  • 11. A Simple program to explain pointers:
  • 12. Pointer Arithmetic  16 bit Machine ( Turbo C ) In a 16 bit machine, size of all types of pointer, be it int*, float*, char* or double* is always 2 bytes. But when we perform any arithmetic function like increment on a pointer, changes occur as per the size of their primitive data type.
  • 14. Examples for Pointer Arithmetic In the above case, pointer will be of 2 bytes. And when we increment it, it will increment by 2 bytes because int is also of 2 bytes. In this case, size of pointer is still 2 bytes. But now, when we increment it, it will increment by 4 bytes because float is of 4 bytes. Similarly, in this case, size of pointer is still 2 bytes. But now, when we increment it, it will increment by 8 bytes because its data type is double.
  • 15. 32 bit Machine (Visual Basic C++)  The concept of pointer arithmetic remains exact same, but the size of pointer and various datatypes is different in a 32 bit machine. Pointer in 32 bit machine is of 4 bytes.
  • 16.  And, following is a table for Size of datatypes on 32-bit Machine :  Note: We cannot add two pointers. This is because pointers contain addresses, adding two addresses makes no sense, because you have no idea what it would point to.But we can subtract two pointers.This is because difference between two pointers gives the number of elements of its data type that can be stored between the two pointers.
  • 18.  Explanation of the above program:  Point 1: Here, * means 'value at the given address'. Thus, it adds the value of m and n which is 15.  Point 2: It subtracts the addresses of the two variables and then divides it by the size of the pointer datatype (here integer, which has size of 4 bytes) which gives us the number of elements of integer data type that can be stored within it.  Point 3: It increments the address stored by the pointer by the size of its datatype(here 4).  Point 4: It decrements the address stored by the pointer by the size of its datatype(here 4).  Point 5: Addition of two pointers is not allowed.
  • 19. Pointer and Arrays  When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of the array. Base address i.e address of the first element of the array is also allocated by the compiler.  Suppose we declare an array arr,
  • 20.  Assuming that the base address of arr is 1000 and each integer requires two bytes, the five elements will be stored as follows:  Here variable arr will give the base address, which is a constant pointer pointing to the element, arr[0].  Therefore arr is containing the address of arr[0] i.e 1000.  In short, arr has two purpose - it is the name of an array and it acts as a pointer pointing towards the first element in the array. arr is equal to &arr[0] //by default
  • 21.  We can declare a pointer of type int to point to the array arr.  Now we can access every element of array arr using p++ to move from one element to another. NOTE : You cannot decrement a pointer once incremented. p-- won't work.
  • 22. Example void main() { int x[5]={2,4,6,8,10},i=0; clrscr(); printf(“nElement Number Element Address”); while(i<5) { printf(“nx[%d] = t%d %u”,i,*(x+i),x+i); i++; }
  • 23. Output Element Number Element Address X[0]= 2 4056 X[1]= 4 4058 X[2]= 6 4060 X[3]= 8 4062 X[4]= 10 4064
  • 24. Pointer to Array  we can use a pointer to point to an Array, and then we can use that pointer to access the array.  Example:  In the above program, the pointer *p will print all the values stored in the array one by one. We can also use the Base address (a in above case) to act as pointer and print all the values.
  • 26.  The generalized form for using pointer with an array, is same as:
  • 27. Array of pointers Example, which uses an array of 3 integers − When the above code is compiled and executed, it produces the following result −
  • 28.  There may be a situation when we want to maintain an array, which can store pointers to an int or char or any other data type available. Following is the declaration of an array of pointers to an integer −  It declares ptr as an array of MAX integer pointers. Thus, each element in ptr, holds a pointer to an int value.
  • 29.  The following example uses three integers, which are stored in an array of pointers, as follows − When the above code is compiled and executed, it produces the following result −
  • 30. Pointers to Function  It is possible to declare a pointer pointing to a function which can then be used as an argument in another function. A pointer to a function is declared as follows,
  • 31.  A function pointer can point to a specific function when it is assigned the name of the function.  s is a pointer to a function sum. Now sum can be called using function pointer s with the list of parameter.
  • 32. Example of Pointer to Function