SlideShare a Scribd company logo
POINTERS
Presented by
Er. Jasleen Kaur
Assistant Professor
Applied Science(CSE)
Chandigarh University
Gharuan (Mohali).
Pointer
• A variable that holds a memory address.
• This address is the location of another object
in the memory.
• Pointer as an address indicates where to find
an object.
Not all pointers actually contain an address
example NULL pointer.
Value of NULL pointer is 0.
10/19/2015
• Pointer can have three kinds of content in it
1) The address of an object, which can be
dereferenced.
2) A NULL pointer.
3) Invalid content, which does not point to an
object.
(If p does not hold a valid value, it can crash the
program)
• If p is a pointer to integer, then
– Int *p
It is possible in some environments to have
multiple pointer values with different
representations that point to same location in
memory.
But make sure if the memory is deleted using
delete or if original variable goes out of scope.
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
Address-of operator(&)
• It is used to reference the memory address of
a variable.
• When we declare a variable, 3 things happen
– Computer memory is set aside for variable
– Variable name is linked to that location in memory
– Value of variable is placed into the memory that
was set aside.
Int *ptr;
declaring variable ptr which holds the value
at address of int type
 int val =1;
assigning int the literal value of 1
 ptr=&val;
dereference and get value at address stored in
ptr
int deref =*ptr
printf(“%dn”, deref);
Output will be 1
Pointer Conversions
• One type of pointer can be converted to
another type of pointer.
• int main() {
double x=100.1, y;
int *p;
p= (int *) &x; //explicit type conversion
y= *p;
}
Generic Pointer
void * pointer is called as generic pointer.
Can’t convert void *pointer to another pointer
and vice-versa.
void *pointer can be assigned to any other
type of pointer.
void * is used to specify a pointer whose base
type is unknown.
It is capable of receiving any type of pointer
argument without reporting any type of
mismatch.
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.
Benefits of pointer
• Pointers are used in situations when passing actual values is
difficult or not desired.
• To return more than one value from a function.
• They increase the execution speed.
• The pointer are more efficient in handling the data types .
• Pointers reduce the length and complexity of a program.
• The use of a pointer array to character string results in
saving of data.
• To allocate memory and access it( Dynamic memory
Allocation).
• Implementing linked lists, trees graphs and many other data
structure.
How to get address of a function
/*A program to get address of a function */
#include<stdio.h>
void main()
{
void show(); /* usual way of invoking a function */
printf(“ The address of show function is=%u”, show);
}
void show()
{
printf(“ welcome to HPES!!”)
}
Uses of pointer to function
• Pointers are certainly awkward and off-putting and thus
this feature of pointer is used for invoking a function
• There are several possible uses :
(a) In writing memory resident program.
(b) In writing viruses, or vaccines to
remove the viruses.
(c) In developing COM/DCOM component
(d) In VC++ programming to connect events to
function calls.
10/19/2015
10/19/2015
10/19/2015
10/19/2015
10/19/2015
10/19/2015
10/19/2015
Thank you

More Related Content

What's hot (20)

PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPT
File handling in c
David Livingston J
 
PPT
Array in c
Ravi Gelani
 
PPTX
Control statements in c
Sathish Narayanan
 
PPTX
Pointers in c++
sai tarlekar
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPTX
Pointers in c language
Tanmay Modi
 
PPTX
Structure in C
Kamal Acharya
 
PPT
Variables in C Programming
programming9
 
PPT
structure and union
student
 
PPTX
Strings in C
Kamal Acharya
 
PPTX
Recursive Function
Harsh Pathak
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
Union in C programming
Kamal Acharya
 
PPTX
class and objects
Payel Guria
 
PPTX
Dynamic memory allocation in c
lavanya marichamy
 
PDF
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
PPTX
Pointers in c++
Vineeta Garg
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PPTX
Storage class in C Language
Nitesh Kumar Pandey
 
Function in C program
Nurul Zakiah Zamri Tan
 
File handling in c
David Livingston J
 
Array in c
Ravi Gelani
 
Control statements in c
Sathish Narayanan
 
Pointers in c++
sai tarlekar
 
C Programming: Control Structure
Sokngim Sa
 
Pointers in c language
Tanmay Modi
 
Structure in C
Kamal Acharya
 
Variables in C Programming
programming9
 
structure and union
student
 
Strings in C
Kamal Acharya
 
Recursive Function
Harsh Pathak
 
Union in C programming
Kamal Acharya
 
class and objects
Payel Guria
 
Dynamic memory allocation in c
lavanya marichamy
 
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
Pointers in c++
Vineeta Garg
 
Data types in c++
Venkata.Manish Reddy
 
Storage class in C Language
Nitesh Kumar Pandey
 

Similar to Pointers in C Programming (20)

PPTX
Pointers and Array, pointer and String.pptx
Ananthi Palanisamy
 
PPTX
Chp3(pointers ref)
Mohd Effandi
 
PDF
Pointers-Computer programming
nmahi96
 
PPTX
FYBSC(CS)_UNIT-1_Pointers in C.pptx
sangeeta borde
 
PPTX
pointer.pptx module of information technology
jolynetomas
 
PPTX
Pointers_in_c_2024.01.10_embedded _c.pptx
ahmedbadr608094
 
PPTX
Lab 3.pptx
MohammedAlobaidy16
 
PPTX
Lab 3.pptx
MohammedAlobaidy16
 
PPT
pointers (1).ppt
Osmania University
 
PDF
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
PPT
358 33 powerpoint-slides_3-pointers_chapter-3
sumitbardhan
 
PPTX
Pointers in C
Vijayananda Ratnam Ch
 
PDF
PROBLEM SOLVING USING PPSC- UNIT -4.pdf
JNTUK KAKINADA
 
PPT
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Rai University
 
PPTX
Lecture 7_Pointer.pptx FOR EDUCATIONAL PURPOSE
famidrabbifr
 
PPTX
Pointer.pptx
SwapnaliPawar27
 
PPT
pointer, structure ,union and intro to file handling
Rai University
 
PPTX
4 Pointers.pptx
aarockiaabinsAPIICSE
 
PPT
pointer, structure ,union and intro to file handling
Rai University
 
Pointers and Array, pointer and String.pptx
Ananthi Palanisamy
 
Chp3(pointers ref)
Mohd Effandi
 
Pointers-Computer programming
nmahi96
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
sangeeta borde
 
pointer.pptx module of information technology
jolynetomas
 
Pointers_in_c_2024.01.10_embedded _c.pptx
ahmedbadr608094
 
Lab 3.pptx
MohammedAlobaidy16
 
Lab 3.pptx
MohammedAlobaidy16
 
pointers (1).ppt
Osmania University
 
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
358 33 powerpoint-slides_3-pointers_chapter-3
sumitbardhan
 
Pointers in C
Vijayananda Ratnam Ch
 
PROBLEM SOLVING USING PPSC- UNIT -4.pdf
JNTUK KAKINADA
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Rai University
 
Lecture 7_Pointer.pptx FOR EDUCATIONAL PURPOSE
famidrabbifr
 
Pointer.pptx
SwapnaliPawar27
 
pointer, structure ,union and intro to file handling
Rai University
 
4 Pointers.pptx
aarockiaabinsAPIICSE
 
pointer, structure ,union and intro to file handling
Rai University
 
Ad

More from Jasleen Kaur (Chandigarh University) (19)

PPTX
Graphs data structures
Jasleen Kaur (Chandigarh University)
 
PPTX
B+ trees and height balance tree
Jasleen Kaur (Chandigarh University)
 
PDF
Operating system notes pdf
Jasleen Kaur (Chandigarh University)
 
PPTX
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
Jasleen Kaur (Chandigarh University)
 
PDF
03 function overloading
Jasleen Kaur (Chandigarh University)
 
PPTX
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
PDF
Remote desktop connection
Jasleen Kaur (Chandigarh University)
 
PPTX
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
PPTX
Calculating garbage value in case of overflow
Jasleen Kaur (Chandigarh University)
 
PPTX
License Plate recognition
Jasleen Kaur (Chandigarh University)
 
PPTX
Afforestation environmental issue
Jasleen Kaur (Chandigarh University)
 
PPTX
Data aggregation in wireless sensor networks
Jasleen Kaur (Chandigarh University)
 
Graphs data structures
Jasleen Kaur (Chandigarh University)
 
B+ trees and height balance tree
Jasleen Kaur (Chandigarh University)
 
Operating system notes pdf
Jasleen Kaur (Chandigarh University)
 
Priority Based Congestion Avoidance Hybrid Scheme published in IEEE
Jasleen Kaur (Chandigarh University)
 
03 function overloading
Jasleen Kaur (Chandigarh University)
 
Chapter 2.datatypes and operators
Jasleen Kaur (Chandigarh University)
 
Remote desktop connection
Jasleen Kaur (Chandigarh University)
 
Operators in C Programming
Jasleen Kaur (Chandigarh University)
 
Calculating garbage value in case of overflow
Jasleen Kaur (Chandigarh University)
 
License Plate recognition
Jasleen Kaur (Chandigarh University)
 
Afforestation environmental issue
Jasleen Kaur (Chandigarh University)
 
Data aggregation in wireless sensor networks
Jasleen Kaur (Chandigarh University)
 
Ad

Recently uploaded (20)

PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Distribution reservoir and service storage pptx
dhanashree78
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Design Thinking basics for Engineers.pdf
CMR University
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 

Pointers in C Programming

  • 1. POINTERS Presented by Er. Jasleen Kaur Assistant Professor Applied Science(CSE) Chandigarh University Gharuan (Mohali).
  • 2. Pointer • A variable that holds a memory address. • This address is the location of another object in the memory. • Pointer as an address indicates where to find an object. Not all pointers actually contain an address example NULL pointer. Value of NULL pointer is 0. 10/19/2015
  • 3. • Pointer can have three kinds of content in it 1) The address of an object, which can be dereferenced. 2) A NULL pointer. 3) Invalid content, which does not point to an object. (If p does not hold a valid value, it can crash the program) • If p is a pointer to integer, then – Int *p
  • 4. It is possible in some environments to have multiple pointer values with different representations that point to same location in memory. But make sure if the memory is deleted using delete or if original variable goes out of scope.
  • 5. 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.
  • 6. 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.
  • 7. 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
  • 8. Address-of operator(&) • It is used to reference the memory address of a variable. • When we declare a variable, 3 things happen – Computer memory is set aside for variable – Variable name is linked to that location in memory – Value of variable is placed into the memory that was set aside.
  • 9. Int *ptr; declaring variable ptr which holds the value at address of int type  int val =1; assigning int the literal value of 1  ptr=&val; dereference and get value at address stored in ptr int deref =*ptr printf(“%dn”, deref); Output will be 1
  • 10. Pointer Conversions • One type of pointer can be converted to another type of pointer. • int main() { double x=100.1, y; int *p; p= (int *) &x; //explicit type conversion y= *p; }
  • 11. Generic Pointer void * pointer is called as generic pointer. Can’t convert void *pointer to another pointer and vice-versa. void *pointer can be assigned to any other type of pointer. void * is used to specify a pointer whose base type is unknown. It is capable of receiving any type of pointer argument without reporting any type of mismatch.
  • 12. 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.
  • 13. • 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.
  • 14. 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.
  • 15. 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.
  • 16. Benefits of pointer • Pointers are used in situations when passing actual values is difficult or not desired. • To return more than one value from a function. • They increase the execution speed. • The pointer are more efficient in handling the data types . • Pointers reduce the length and complexity of a program.
  • 17. • The use of a pointer array to character string results in saving of data. • To allocate memory and access it( Dynamic memory Allocation). • Implementing linked lists, trees graphs and many other data structure.
  • 18. How to get address of a function /*A program to get address of a function */ #include<stdio.h> void main() { void show(); /* usual way of invoking a function */ printf(“ The address of show function is=%u”, show); } void show() { printf(“ welcome to HPES!!”) }
  • 19. Uses of pointer to function • Pointers are certainly awkward and off-putting and thus this feature of pointer is used for invoking a function • There are several possible uses : (a) In writing memory resident program. (b) In writing viruses, or vaccines to remove the viruses. (c) In developing COM/DCOM component (d) In VC++ programming to connect events to function calls.