SlideShare a Scribd company logo
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
Most read
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
Most read
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
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

What's hot (20)

C Pointers
C PointersC Pointers
C Pointers
omukhtar
 
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Polish Notation In Data Structure
Polish Notation In Data StructurePolish Notation In Data Structure
Polish Notation In Data Structure
Meghaj Mallick
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
verisan
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
Viji B
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
yazad dumasia
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
lavanya marichamy
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
Samiksha Pun
 
Pointer arithmetic in c
Pointer arithmetic in c Pointer arithmetic in c
Pointer arithmetic in c
sangrampatil81
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
Wingston
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
Nitesh Kumar Pandey
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
arushi bhatnagar
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
pointers
pointerspointers
pointers
teach4uin
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 

Viewers also liked (14)

Pointers in c
Pointers in cPointers in c
Pointers in c
Mohd Arif
 
Pointers in C
Pointers in CPointers in C
Pointers in C
guestdc3f16
 
C programming - Pointer and DMA
C programming - Pointer and DMAC programming - Pointer and DMA
C programming - Pointer and DMA
Achyut Devkota
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Prabhu Govind
 
Pointers
PointersPointers
Pointers
sanya6900
 
Structure c
Structure cStructure c
Structure c
thirumalaikumar3
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
Agnas Jasmine
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
Dhrumil Patel
 
C Structures & Unions
C Structures & UnionsC Structures & Unions
C Structures & Unions
Ram Sagar Mourya
 
Module 3 Scanning
Module 3   ScanningModule 3   Scanning
Module 3 Scanning
leminhvuong
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
J.T.A.JONES
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
Chaand Sheikh
 
C Structures And Unions
C  Structures And  UnionsC  Structures And  Unions
C Structures And Unions
Ram Sagar Mourya
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
Ad

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

PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
Ramakrishna Reddy Bijjam
 
pointer.pptx module of information technology
pointer.pptx module of information technologypointer.pptx module of information technology
pointer.pptx module of information technology
jolynetomas
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c languageUNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
Pointers
PointersPointers
Pointers
Frijo Francis
 
Introduction to pointers in c plus plus .
Introduction to pointers in c plus plus .Introduction to pointers in c plus plus .
Introduction to pointers in c plus plus .
karimibaryal1996
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
janithlakshan1
 
Pointers in C++ object oriented programming
Pointers in C++ object oriented programmingPointers in C++ object oriented programming
Pointers in C++ object oriented programming
Ahmad177077
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
aarockiaabinsAPIICSE
 
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
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
sajinis3
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
Vikram Nandini
 
Pointers
PointersPointers
Pointers
Vardhil Patel
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejejeUnit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
SwapnaliPawar27
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
s170883BesiVyshnavi
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
Ramakrishna Reddy Bijjam
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi96
 
Pointers
PointersPointers
Pointers
Lp Singh
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
Ramakrishna Reddy Bijjam
 
pointer.pptx module of information technology
pointer.pptx module of information technologypointer.pptx module of information technology
pointer.pptx module of information technology
jolynetomas
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c languageUNIT 4 POINTERS.pptx pointers pptx for basic c language
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
Introduction to pointers in c plus plus .
Introduction to pointers in c plus plus .Introduction to pointers in c plus plus .
Introduction to pointers in c plus plus .
karimibaryal1996
 
Pointers in C++ object oriented programming
Pointers in C++ object oriented programmingPointers in C++ object oriented programming
Pointers in C++ object oriented programming
Ahmad177077
 
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
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
sajinis3
 
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejejeUnit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
Unit-4-1.pptxjtjrjfjfjfjfjfjfjfjrjrjrjrjejejeje
KathanPatel49
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
Ramakrishna Reddy Bijjam
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi96
 
Ad

More from Jayanshu Gundaniya (15)

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

Recently uploaded (20)

Build Smarter, Deliver Faster with Choreo - An AI Native Internal Developer P...
Build Smarter, Deliver Faster with Choreo - An AI Native Internal Developer P...Build Smarter, Deliver Faster with Choreo - An AI Native Internal Developer P...
Build Smarter, Deliver Faster with Choreo - An AI Native Internal Developer P...
WSO2
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlowDevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Key AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence CompaniesKey AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence Companies
Mypcot Infotech
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
Best Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small BusinessesBest Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small Businesses
TheTelephony
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native BarcelonaOpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdfThe Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
Varsha Nayak
 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI SearchAgentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
 
Software Testing & it’s types (DevOps)
Software  Testing & it’s  types (DevOps)Software  Testing & it’s  types (DevOps)
Software Testing & it’s types (DevOps)
S Pranav (Deepu)
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across IndustriesArtificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps CyclesFrom Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
Marjukka Niinioja
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdfdp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
Automating Map Production With FME and Python
Automating Map Production With FME and PythonAutomating Map Production With FME and Python
Automating Map Production With FME and Python
Safe Software
 
FME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable InsightsFME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable Insights
Safe Software
 
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Natan Silnitsky
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 WebinarPorting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?
felipeceotto
 
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The SequelMarketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
BradBedford3
 
Revolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management SoftwareRevolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management Software
Insurance Tech Services
 
Build Smarter, Deliver Faster with Choreo - An AI Native Internal Developer P...
Build Smarter, Deliver Faster with Choreo - An AI Native Internal Developer P...Build Smarter, Deliver Faster with Choreo - An AI Native Internal Developer P...
Build Smarter, Deliver Faster with Choreo - An AI Native Internal Developer P...
WSO2
 
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlowDevOps for AI: running LLMs in production with Kubernetes and KubeFlow
DevOps for AI: running LLMs in production with Kubernetes and KubeFlow
Aarno Aukia
 
Key AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence CompaniesKey AI Technologies Used by Indian Artificial Intelligence Companies
Key AI Technologies Used by Indian Artificial Intelligence Companies
Mypcot Infotech
 
Agile Software Engineering Methodologies
Agile Software Engineering MethodologiesAgile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
 
Best Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small BusinessesBest Inbound Call Tracking Software for Small Businesses
Best Inbound Call Tracking Software for Small Businesses
TheTelephony
 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native BarcelonaOpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
 
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdfThe Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
Varsha Nayak
 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI SearchAgentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
 
Software Testing & it’s types (DevOps)
Software  Testing & it’s  types (DevOps)Software  Testing & it’s  types (DevOps)
Software Testing & it’s types (DevOps)
S Pranav (Deepu)
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across IndustriesArtificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps CyclesFrom Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
From Chaos to Clarity - Designing (AI-Ready) APIs with APIOps Cycles
Marjukka Niinioja
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdfdp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
 
Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4Software Engineering Process, Notation & Tools Introduction - Part 4
Software Engineering Process, Notation & Tools Introduction - Part 4
Gaurav Sharma
 
Automating Map Production With FME and Python
Automating Map Production With FME and PythonAutomating Map Production With FME and Python
Automating Map Production With FME and Python
Safe Software
 
FME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable InsightsFME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable Insights
Safe Software
 
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Natan Silnitsky
 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 WebinarPorting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
 
GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?
felipeceotto
 
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The SequelMarketo & Dynamics can be Most Excellent to Each Other – The Sequel
Marketo & Dynamics can be Most Excellent to Each Other – The Sequel
BradBedford3
 
Revolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management SoftwareRevolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management Software
Insurance Tech Services
 

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.