SlideShare a Scribd company logo
6
Most read
8
Most read
19
Most read
Introduction to Pointers 
• A Pointer is a derived data type in ‘C’ . 
• It is built from one of the fundamental data types available in 
‘C’ . 
• Pointers contain the memory addresses as their values . 
• Memory addresses, being the location of computer memory, 
can be accessed & used to store data via pointers .
Understanding Pointers 
• During the whole program execution 
the variable num is associated with the 
address 6843. This value of address, 
being a simple integer, can bee stored in 
another variable which is called pointer. 
• Pointer, again, is stored in some 
another memory location 6894 which 
too is accessible. 
• The link between address & value of 
variable can be visualized with the help 
of pointer in figure.
 The term instructs the system to find a location for 
integer variable ‘a’ and assign 100 value in that location. 
 Pointers, on the other side, spot the address or location area of the 
variable and not directly on the intermediate value of that variable. 
 The coding… 
…reflects how to declare a pointer variable. 
1. Using asterisk ‘*’ with the data type before the variable name 
declares it as a ‘pointer’ . 
2. The address operator ‘&’ assigns the address of the specified 
variable to the pointer variable. 
3. Variable that hasn’t been assigned any value may contain garbage 
and make the pointer point to unknown locations.
• Pointer variables can be initialized either in their 
declaration part OR in between a couple of 
(The variable must be function statements 
declared before the 
initialization. Also the 
data type of pointer variable 
& the variable to which 
it is assigned should be the same.) 
• Pointers, being flexible, can be used in different ways 
A single pointer A single variable to 
to many variables in different many pointers 
statements
• Pointers may be used to assign a value to a variable based on the 
other one like… 
…assigns 223 to ‘n’ in two ways 
1. By using the pointer to extract the value stored in ‘a’ . 
2. By directly using the address of ‘a’ . 
 NOTE : A value stored in address 4243 or any other 
can’t be accessed by ‘ *4243 ‘ .
6 
Understanding Pointers by Examples 
x : 4892 
ip : 4904 
int x = 70, y = 80, z[4] = {10, 20, 30, 40 }; 
int *ip; // int pointer ip 
ip = &x; // ip is assigned to address of x 
*ip = 200; // content of ip is assigned to 200 
y = *ip; // y is assigned to content of ip 
ip = &z[2]; 
*ip = *ip + 20; // same as *ip += 20; 
y = *ip+1; 
y : 4894 
Z, Z[0] : 4896 
Z[1] : 4898 
Z[2] : 4900 
Z[3] : 4902 
200 
70 
200 
80 
51 
10 
20 
30 
50 
40 
???? 
4892 
4900
Pointer to Pointer 
• Pointer itself are variables whose locations are specifies on memory 
and their storage address too can be known by assigning a pointer. 
• We can access a target value indirectly pointed to by a pointer by 
applying the indirection operator or the asterisk mark twice. 
… ‘a’ is assigned a value ‘100’ and it’s location stored in ‘p1’ 
whose location in turn is stored in ‘p2’ . ‘*p1’ refers to ‘100’ so does 
‘**p2’ . 
• REMEMBER to assign similar data types to chain pointing variables.
Pointer Expressions 
• Arithmetic operations between two or more pointer is not possible. 
• But pointers can be used to perform arithmetic operations on the value 
they point to. 
e.g.: …same as ((*p1) * (*p2)) / (*p3) 
…same as (10 * (-(*p3))) / (*p2) 
 Note to keep a space between / and * to not to make compiler interpret 
it to be a comment. 
• Pointer incrementation is valid in ‘C’ . 
e.g.: p++; OR p=p1+2; are valid statements . 
• A pointer, when incremented, it increases it’s value by the length of the 
data type it points to. 
1. characters – 1 byte 3. Float – 4 bytes 
2. integer – 2 bytes 4. double – 8 bytes
Illustration Of ‘Pointer to Pointer’ + ‘Expressions using Pointer’ 
1. int a,b,c,*p,**q; 
2. a=10; 
3. b=20; 
4. c=30; 
5. printf(“%d %d %d”,a,b,c); 
6. p=&a; 
7. q=&p; 
8. b=b/ (( *p * **q ) / 10); 
9. c=c+ ( 2 * *p) - **q; 
10. printf(“n%d %d %d”,a,b,c); 
Output: 
10 20 30 
10 2 40 
b= 20/ ( ( ( value indicated by pointer p) * ( 
value indicated by chain pointer q ) ) 
/ 10 ) 
c=30 + (2 * (value indicated by pointer p) ) 
- ( value indicated by chain pointer q )
Pointer & Arrays 
• The compiler, by default, allocates sufficient amount of storage to 
contain all elements when an array is declared. 
• These memory locations are contiguous as shown below. 
Elements 
Value 
Address 
a[0] a[1] a[2] a[3] a[4] 
31 24 43 6 13 
1030 1032 1034 1036 1038 
• The memory address increases by the bits of data the data type of 
the variable occupies.
• These memory locations, being contiguous, can be used by pointers 
to access the exact locations of any specific variable of an array. 
E.g. :- 
int a[5],*p; 
p=a; /* by default p is the address of a[0] */ 
p+1=4; /* assigning ‘4’ to a[1], shown by ‘p+1’ */ 
p+2=12; /* assigning ‘12’ to a[3], shown by ‘p+2’ */ 
p+3=10; /* assigning ‘10’ to a[2], shown by ‘p+3’ */ 
• Also a[1], a[2],etc. can be directly referred by using *(p+1), *(p+2), 
etc. 
Pointer & Arrays
Examples of ‘Arithmetic Operation On Pointer’ 
as well as ‘Pointers & Arrays’ 
float a[4]; 
float *ptr; 
ptr = &(a[2]); 
*ptr = 3.14; 
ptr++; 
*ptr = 9.0; 
ptr = ptr - 3; 
*ptr = 6.0; 
ptr += 2; 
*ptr = 7.0; 
Data Table 
Name Type Description Value 
a[0] float float array element (variable) ? 
a[1] float float array element (variable) ? 
a[2] float float array element (variable) ? 
a[3] float float array element (variable) ? 
ptr float * float pointer variable 
*ptr float de-reference of float pointer 
variable 
3.14 
7.0 
address of a[2] 
3.14 
? 
3] 
9.0 
9.0 
0] 
6.0 
6.0 
7.0
Pointer & Functions : Pointer as function 
arguments 
• By using pointer as parameter, addresses of variables is passed to the called 
function. This process of calling a function to pass address of variables is 
called ‘Call By Reference’ OR ‘Pass By Pointers’ . 
• The function called by ‘reference’ can change the value of the variable used 
in the call. 
• E.g. :- 
The function value() receives the address of 
variable a & not the value. Inside value(), a is 
pointer & therefore it increments the value of 
variable a by 50. 
OUTPUT : 
70
Pointer & Functions : Function Returning 
• As pointers are a data type in ‘C’ , a function can return a pointer to the 
calling function. 
• E.g. :- 
The coding aside shows the function 
addvalue() receiving address of a as a 
parameter. It increments the value stored in 
the address of a & then returns that specific 
address to the calling function, which is 
then assigned to pointer variable p. 
OUTPUT :- 
40 
Pointers
Pointer & Functions : Pointers to Functions 
• Function too has an address location as well as a type in the memory. So, it 
is thereby possible to use pointer to point to a specific function, which can 
then be used as argument in another function. 
• The declaration of pointer to a function takes place as follows: 
data_type (*pointer_name) (data); 
 Here, the data type specifies must be the same the function, which the 
pointer points to, is going to return. 
 Moreover a pointer can be assigned to a function by simply equating the 
pointer name to the name of the function. 
e.g. :- 
float add(int, int); 
float (*p) (int, int); 
p=add; 
 A function can also be called using pointer like :- 
(*p)(a,b); /* equivalent to [ add(x,y); ] */
Pointer & Functions : Pointers to Functions 
• An Illustration to add two integral numbers :- 
#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 
int (*p)(int, int); /*declaration of function pointer ‘p’ which points function ‘add ‘*/ 
void print(int (*p)(int, int)); /* declaration of function ‘print’ */ 
int add(int, int); /* declaration of function ‘add’ */ 
void main() 
{ 
p=add; /* initializing pointer */ 
print(p); /* calling function ‘print’ which receives the address of 
function ‘add’ through pointer ‘p’ */ 
} /* Continued */
Pointer & Functions : Pointers to Functions 
• An Illustration to add two integral numbers (continued) :- 
OUTPUT 
void printf(int (*p)(int, int)) 
{ 
int a,b; 
scanf(“%d %d”,&a,&b); 
printf(“n%d”,(*p)(a,b)); /* passes values of ‘a’ & ‘b’ to ‘add’ through ‘p’ */ 
} 
int add(int a, int b) 
{ 
return(a+b); /* adds ‘a’ & ‘b’ */ 
} 
/* program over */ 
30 50 
80
Uses of Pointers 
i. Pointers can be used to return multiple values from a function 
via function arguments . 
ii. They prove to be an efficient tool for manipulating dynamic 
data structures such as Linked Lists, Queens, Stacks & Trees. 
iii. They reduce the program execution speed as well as their 
altitude of complexity . 
iv. Pointers save a lot of data storage space in memory when 
used with character strings
Pitfalls Of Pointer 
• Since Pointer holds addresses of memory 
location, it must never be used without proper 
initialization. 
• An uninitialized pointer may hold addresses of 
some memory location that is protected by the 
Operating System. In such cases, de-referencing 
a pointer may crash the program. 
• Pointer can’t track the boundaries of an array.

More Related Content

PPTX
Pointers in c++
sai tarlekar
 
PPTX
Tuple in python
Sharath Ankrajegowda
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PPTX
Union in C programming
Kamal Acharya
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPT
structure and union
student
 
PPTX
Pointers in c++
Rajat Busheheri
 
PPTX
Union in c language
tanmaymodi4
 
Pointers in c++
sai tarlekar
 
Tuple in python
Sharath Ankrajegowda
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Union in C programming
Kamal Acharya
 
Function in C program
Nurul Zakiah Zamri Tan
 
structure and union
student
 
Pointers in c++
Rajat Busheheri
 
Union in c language
tanmaymodi4
 

What's hot (20)

PPTX
Control statements in c
Sathish Narayanan
 
PPTX
Structure in C
Kamal Acharya
 
PPTX
Strings in C
Kamal Acharya
 
PPTX
Functions in C
Kamal Acharya
 
PPTX
Pointer in c program
Rumman Ansari
 
PPTX
Preprocessor directives in c language
tanmaymodi4
 
PPT
Pointers C programming
Appili Vamsi Krishna
 
PPTX
Pointers in c language
Tanmay Modi
 
PPTX
Pointers in c - Mohammad Salman
MohammadSalman129
 
PPTX
POINTERS IN C
Neel Mungra
 
PPTX
C pointer
University of Potsdam
 
PDF
Python-01| Fundamentals
Mohd Sajjad
 
PDF
Pointers
sarith divakar
 
PPTX
Stacks in c++
Vineeta Garg
 
PPTX
Data Structures in Python
Devashish Kumar
 
PPTX
Arrays in c
Jeeva Nanthini
 
PPTX
Pointer in c
lavanya marichamy
 
PPTX
Polymorphism in c++(ppt)
Sanjit Shaw
 
PDF
Dbms 14: Relational Calculus
Amiya9439793168
 
PPTX
Type conversion
PreethaPreetha5
 
Control statements in c
Sathish Narayanan
 
Structure in C
Kamal Acharya
 
Strings in C
Kamal Acharya
 
Functions in C
Kamal Acharya
 
Pointer in c program
Rumman Ansari
 
Preprocessor directives in c language
tanmaymodi4
 
Pointers C programming
Appili Vamsi Krishna
 
Pointers in c language
Tanmay Modi
 
Pointers in c - Mohammad Salman
MohammadSalman129
 
POINTERS IN C
Neel Mungra
 
Python-01| Fundamentals
Mohd Sajjad
 
Pointers
sarith divakar
 
Stacks in c++
Vineeta Garg
 
Data Structures in Python
Devashish Kumar
 
Arrays in c
Jeeva Nanthini
 
Pointer in c
lavanya marichamy
 
Polymorphism in c++(ppt)
Sanjit Shaw
 
Dbms 14: Relational Calculus
Amiya9439793168
 
Type conversion
PreethaPreetha5
 
Ad

Viewers also liked (15)

PPT
Pointers in c
Mohd Arif
 
PPT
Pointers in C
guestdc3f16
 
PPTX
C programming - Pointer and DMA
Achyut Devkota
 
PDF
C Pointers
omukhtar
 
PPT
Pointers in C
Prabhu Govind
 
PPT
Pointers
sanya6900
 
PPT
Structure c
thirumalaikumar3
 
PPT
Disk scheduling
Agnas Jasmine
 
PPTX
C Structures and Unions
Dhrumil Patel
 
PPT
C Structures & Unions
Ram Sagar Mourya
 
PPT
Module 3 Scanning
leminhvuong
 
PPT
Disk scheduling
J.T.A.JONES
 
PPTX
C++ Pointers
Chaand Sheikh
 
PPT
C Structures And Unions
Ram Sagar Mourya
 
PPT
Unit 6 pointers
George Erfesoglou
 
Pointers in c
Mohd Arif
 
Pointers in C
guestdc3f16
 
C programming - Pointer and DMA
Achyut Devkota
 
C Pointers
omukhtar
 
Pointers in C
Prabhu Govind
 
Pointers
sanya6900
 
Structure c
thirumalaikumar3
 
Disk scheduling
Agnas Jasmine
 
C Structures and Unions
Dhrumil Patel
 
C Structures & Unions
Ram Sagar Mourya
 
Module 3 Scanning
leminhvuong
 
Disk scheduling
J.T.A.JONES
 
C++ Pointers
Chaand Sheikh
 
C Structures And Unions
Ram Sagar Mourya
 
Unit 6 pointers
George Erfesoglou
 
Ad

Similar to Basics of pointer, pointer expressions, pointer to pointer and pointer in functions (20)

PDF
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
PPTX
Pointers and single &multi dimentionalarrays.pptx
Ramakrishna Reddy Bijjam
 
PPTX
pointer.pptx module of information technology
jolynetomas
 
PPTX
Pointer in C
bipchulabmki
 
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
PPSX
Pointers
Frijo Francis
 
PPTX
Introduction to pointers in c plus plus .
karimibaryal1996
 
PPTX
pointers.pptx
janithlakshan1
 
PPTX
Pointers in C++ object oriented programming
Ahmad177077
 
PPTX
4 Pointers.pptx
aarockiaabinsAPIICSE
 
PDF
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
sudhakargeruganti
 
PPTX
Unit-I Pointer Data structure.pptx
ajajkhan16
 
PPTX
PPS-POINTERS.pptx
sajinis3
 
PPTX
C Programming Unit-4
Vikram Nandini
 
PPTX
Pointers
Vardhil Patel
 
PPTX
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
PPTX
Pointer.pptx
SwapnaliPawar27
 
PPTX
pointers.pptx
s170883BesiVyshnavi
 
PPTX
Arrays to arrays and pointers with arrays.pptx
Ramakrishna Reddy Bijjam
 
PDF
Pointers-Computer programming
nmahi96
 
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
Pointers and single &multi dimentionalarrays.pptx
Ramakrishna Reddy Bijjam
 
pointer.pptx module of information technology
jolynetomas
 
Pointer in C
bipchulabmki
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
Pointers
Frijo Francis
 
Introduction to pointers in c plus plus .
karimibaryal1996
 
pointers.pptx
janithlakshan1
 
Pointers in C++ object oriented programming
Ahmad177077
 
4 Pointers.pptx
aarockiaabinsAPIICSE
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
sudhakargeruganti
 
Unit-I Pointer Data structure.pptx
ajajkhan16
 
PPS-POINTERS.pptx
sajinis3
 
C Programming Unit-4
Vikram Nandini
 
Pointers
Vardhil Patel
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
Pointer.pptx
SwapnaliPawar27
 
pointers.pptx
s170883BesiVyshnavi
 
Arrays to arrays and pointers with arrays.pptx
Ramakrishna Reddy Bijjam
 
Pointers-Computer programming
nmahi96
 

More from Jayanshu Gundaniya (15)

PPTX
Erbium Doped Fiber Amplifier (EDFA)
Jayanshu Gundaniya
 
PPTX
Three Phase to Three phase Cycloconverter
Jayanshu Gundaniya
 
PPTX
Fourier Series for Continuous Time & Discrete Time Signals
Jayanshu Gundaniya
 
PPT
Comparison of A, B & C Power Amplifiers
Jayanshu Gundaniya
 
PPT
Multiplexers & Demultiplexers
Jayanshu Gundaniya
 
PPT
Initial Conditions of Resistor, Inductor & Capacitor
Jayanshu Gundaniya
 
PPT
First order non-linear partial differential equation & its applications
Jayanshu Gundaniya
 
PPT
Engineering Mathematics - Total derivatives, chain rule and derivative of imp...
Jayanshu Gundaniya
 
PPT
Engineering Graphics - Projection of points and lines
Jayanshu Gundaniya
 
PPT
Internal expanding shoe brake short presentation
Jayanshu Gundaniya
 
PPT
Hydrological cycle
Jayanshu Gundaniya
 
PPT
Ecology and ecosystem
Jayanshu Gundaniya
 
PPT
Architectural acoustics topics and remedies - short presentation
Jayanshu Gundaniya
 
PPT
Superconductors and Superconductivity
Jayanshu Gundaniya
 
PPTX
Superposition theorem
Jayanshu Gundaniya
 
Erbium Doped Fiber Amplifier (EDFA)
Jayanshu Gundaniya
 
Three Phase to Three phase Cycloconverter
Jayanshu Gundaniya
 
Fourier Series for Continuous Time & Discrete Time Signals
Jayanshu Gundaniya
 
Comparison of A, B & C Power Amplifiers
Jayanshu Gundaniya
 
Multiplexers & Demultiplexers
Jayanshu Gundaniya
 
Initial Conditions of Resistor, Inductor & Capacitor
Jayanshu Gundaniya
 
First order non-linear partial differential equation & its applications
Jayanshu Gundaniya
 
Engineering Mathematics - Total derivatives, chain rule and derivative of imp...
Jayanshu Gundaniya
 
Engineering Graphics - Projection of points and lines
Jayanshu Gundaniya
 
Internal expanding shoe brake short presentation
Jayanshu Gundaniya
 
Hydrological cycle
Jayanshu Gundaniya
 
Ecology and ecosystem
Jayanshu Gundaniya
 
Architectural acoustics topics and remedies - short presentation
Jayanshu Gundaniya
 
Superconductors and Superconductivity
Jayanshu Gundaniya
 
Superposition theorem
Jayanshu Gundaniya
 

Recently uploaded (20)

PPTX
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PPTX
Presentation about variables and constant.pptx
safalsingh810
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
oapresentation.pptx
mehatdhavalrajubhai
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Presentation about variables and constant.pptx
safalsingh810
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
Exploring AI Agents in Process Industries
amoreira6
 

Basics of pointer, pointer expressions, pointer to pointer and pointer in functions

  • 1. Introduction to Pointers • A Pointer is a derived data type in ‘C’ . • It is built from one of the fundamental data types available in ‘C’ . • Pointers contain the memory addresses as their values . • Memory addresses, being the location of computer memory, can be accessed & used to store data via pointers .
  • 2. Understanding Pointers • During the whole program execution the variable num is associated with the address 6843. This value of address, being a simple integer, can bee stored in another variable which is called pointer. • Pointer, again, is stored in some another memory location 6894 which too is accessible. • The link between address & value of variable can be visualized with the help of pointer in figure.
  • 3.  The term instructs the system to find a location for integer variable ‘a’ and assign 100 value in that location.  Pointers, on the other side, spot the address or location area of the variable and not directly on the intermediate value of that variable.  The coding… …reflects how to declare a pointer variable. 1. Using asterisk ‘*’ with the data type before the variable name declares it as a ‘pointer’ . 2. The address operator ‘&’ assigns the address of the specified variable to the pointer variable. 3. Variable that hasn’t been assigned any value may contain garbage and make the pointer point to unknown locations.
  • 4. • Pointer variables can be initialized either in their declaration part OR in between a couple of (The variable must be function statements declared before the initialization. Also the data type of pointer variable & the variable to which it is assigned should be the same.) • Pointers, being flexible, can be used in different ways A single pointer A single variable to to many variables in different many pointers statements
  • 5. • Pointers may be used to assign a value to a variable based on the other one like… …assigns 223 to ‘n’ in two ways 1. By using the pointer to extract the value stored in ‘a’ . 2. By directly using the address of ‘a’ .  NOTE : A value stored in address 4243 or any other can’t be accessed by ‘ *4243 ‘ .
  • 6. 6 Understanding Pointers by Examples x : 4892 ip : 4904 int x = 70, y = 80, z[4] = {10, 20, 30, 40 }; int *ip; // int pointer ip ip = &x; // ip is assigned to address of x *ip = 200; // content of ip is assigned to 200 y = *ip; // y is assigned to content of ip ip = &z[2]; *ip = *ip + 20; // same as *ip += 20; y = *ip+1; y : 4894 Z, Z[0] : 4896 Z[1] : 4898 Z[2] : 4900 Z[3] : 4902 200 70 200 80 51 10 20 30 50 40 ???? 4892 4900
  • 7. Pointer to Pointer • Pointer itself are variables whose locations are specifies on memory and their storage address too can be known by assigning a pointer. • We can access a target value indirectly pointed to by a pointer by applying the indirection operator or the asterisk mark twice. … ‘a’ is assigned a value ‘100’ and it’s location stored in ‘p1’ whose location in turn is stored in ‘p2’ . ‘*p1’ refers to ‘100’ so does ‘**p2’ . • REMEMBER to assign similar data types to chain pointing variables.
  • 8. Pointer Expressions • Arithmetic operations between two or more pointer is not possible. • But pointers can be used to perform arithmetic operations on the value they point to. e.g.: …same as ((*p1) * (*p2)) / (*p3) …same as (10 * (-(*p3))) / (*p2)  Note to keep a space between / and * to not to make compiler interpret it to be a comment. • Pointer incrementation is valid in ‘C’ . e.g.: p++; OR p=p1+2; are valid statements . • A pointer, when incremented, it increases it’s value by the length of the data type it points to. 1. characters – 1 byte 3. Float – 4 bytes 2. integer – 2 bytes 4. double – 8 bytes
  • 9. Illustration Of ‘Pointer to Pointer’ + ‘Expressions using Pointer’ 1. int a,b,c,*p,**q; 2. a=10; 3. b=20; 4. c=30; 5. printf(“%d %d %d”,a,b,c); 6. p=&a; 7. q=&p; 8. b=b/ (( *p * **q ) / 10); 9. c=c+ ( 2 * *p) - **q; 10. printf(“n%d %d %d”,a,b,c); Output: 10 20 30 10 2 40 b= 20/ ( ( ( value indicated by pointer p) * ( value indicated by chain pointer q ) ) / 10 ) c=30 + (2 * (value indicated by pointer p) ) - ( value indicated by chain pointer q )
  • 10. Pointer & Arrays • The compiler, by default, allocates sufficient amount of storage to contain all elements when an array is declared. • These memory locations are contiguous as shown below. Elements Value Address a[0] a[1] a[2] a[3] a[4] 31 24 43 6 13 1030 1032 1034 1036 1038 • The memory address increases by the bits of data the data type of the variable occupies.
  • 11. • These memory locations, being contiguous, can be used by pointers to access the exact locations of any specific variable of an array. E.g. :- int a[5],*p; p=a; /* by default p is the address of a[0] */ p+1=4; /* assigning ‘4’ to a[1], shown by ‘p+1’ */ p+2=12; /* assigning ‘12’ to a[3], shown by ‘p+2’ */ p+3=10; /* assigning ‘10’ to a[2], shown by ‘p+3’ */ • Also a[1], a[2],etc. can be directly referred by using *(p+1), *(p+2), etc. Pointer & Arrays
  • 12. Examples of ‘Arithmetic Operation On Pointer’ as well as ‘Pointers & Arrays’ float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) ? a[3] float float array element (variable) ? ptr float * float pointer variable *ptr float de-reference of float pointer variable 3.14 7.0 address of a[2] 3.14 ? 3] 9.0 9.0 0] 6.0 6.0 7.0
  • 13. Pointer & Functions : Pointer as function arguments • By using pointer as parameter, addresses of variables is passed to the called function. This process of calling a function to pass address of variables is called ‘Call By Reference’ OR ‘Pass By Pointers’ . • The function called by ‘reference’ can change the value of the variable used in the call. • E.g. :- The function value() receives the address of variable a & not the value. Inside value(), a is pointer & therefore it increments the value of variable a by 50. OUTPUT : 70
  • 14. Pointer & Functions : Function Returning • As pointers are a data type in ‘C’ , a function can return a pointer to the calling function. • E.g. :- The coding aside shows the function addvalue() receiving address of a as a parameter. It increments the value stored in the address of a & then returns that specific address to the calling function, which is then assigned to pointer variable p. OUTPUT :- 40 Pointers
  • 15. Pointer & Functions : Pointers to Functions • Function too has an address location as well as a type in the memory. So, it is thereby possible to use pointer to point to a specific function, which can then be used as argument in another function. • The declaration of pointer to a function takes place as follows: data_type (*pointer_name) (data);  Here, the data type specifies must be the same the function, which the pointer points to, is going to return.  Moreover a pointer can be assigned to a function by simply equating the pointer name to the name of the function. e.g. :- float add(int, int); float (*p) (int, int); p=add;  A function can also be called using pointer like :- (*p)(a,b); /* equivalent to [ add(x,y); ] */
  • 16. Pointer & Functions : Pointers to Functions • An Illustration to add two integral numbers :- #include<stdio.h> #include<conio.h> #include<stdlib.h> int (*p)(int, int); /*declaration of function pointer ‘p’ which points function ‘add ‘*/ void print(int (*p)(int, int)); /* declaration of function ‘print’ */ int add(int, int); /* declaration of function ‘add’ */ void main() { p=add; /* initializing pointer */ print(p); /* calling function ‘print’ which receives the address of function ‘add’ through pointer ‘p’ */ } /* Continued */
  • 17. Pointer & Functions : Pointers to Functions • An Illustration to add two integral numbers (continued) :- OUTPUT void printf(int (*p)(int, int)) { int a,b; scanf(“%d %d”,&a,&b); printf(“n%d”,(*p)(a,b)); /* passes values of ‘a’ & ‘b’ to ‘add’ through ‘p’ */ } int add(int a, int b) { return(a+b); /* adds ‘a’ & ‘b’ */ } /* program over */ 30 50 80
  • 18. Uses of Pointers i. Pointers can be used to return multiple values from a function via function arguments . ii. They prove to be an efficient tool for manipulating dynamic data structures such as Linked Lists, Queens, Stacks & Trees. iii. They reduce the program execution speed as well as their altitude of complexity . iv. Pointers save a lot of data storage space in memory when used with character strings
  • 19. Pitfalls Of Pointer • Since Pointer holds addresses of memory location, it must never be used without proper initialization. • An uninitialized pointer may hold addresses of some memory location that is protected by the Operating System. In such cases, de-referencing a pointer may crash the program. • Pointer can’t track the boundaries of an array.