SlideShare a Scribd company logo
1 of 17
Pointer Operations
Type Casting:
Converting the data type of one variable into another data type.
Syntax:
(data type to be converted to) variable;
Eg:
float a = 2.3;
int b;
b=(int)a;
Types:
Implicit Conversion:
int float double (small no of bytes higher no of bytes)
Explicit Conversion:
double float int (higher no of bytes small no of bytes)
Converting pointer from one type to another:
 Pointer of one type cannot be implicitly converted from
one type to another but can be explicitly converted using
type casting.
 In such a conversion a pointer always assumes that it is
point to a object of its type but reality may differ.
Syntax:
(data type to be converted to *) variable;
int *p;
(char *)p;
Normal variable cannot be converted to pointer variable
using type casting. Pointer variable of one type can be type
casted to another type.
Notes:
 Type conversion is a powerful feature but yet it may
difficult to remove bugs and crashes and should be used
with uttermost vigilance.
 It may also lead to unexpected and unreliable results but
program would compile successfully.
 While typecasting one pointer to another because even
after type casting the pointer can point to anything but it
will still think it is pointing to something of it declared type
and have properties of the original type.
 void pointer can be used for this purpose. As the void
pointer doesn’t point to any type of data, it can be type
casted to any type, and results in no error.
 Example program to convert the pointer from pointing to int
to char data type.
main()
{
int i = 10;
char *p1;
int *p2;
p2 = &i;
p1 = (char *) p2; // Type Casting and Pointer Conversion
printf (" *p1 = %c And *p2 = %d", *p1,*p2); // Output will be depending upon
the compiler.
}
 Example program to convert the pointer from pointing to
void to char data type.
main()
{
int i = 10;
int *p1;
void *p2;
p2 = &i;
p1 = (int *) p2; // Type Casting and Pointer Conversion
printf (" *p1 = %d And *p2 = %d", *p1,*p2);
}
Output:
*p1 = 10 And *p2 = 10
int a=13;
2 bytes as a whole form the
65345 65346 integer
float q=2.3;
float *s;
s=&q;
*s will give the value 2.3
4 bytes form the
23456 25457 25458 25459 float value
Pointer variable ‘s’ will be assigned with the address of q. The
address of q assigned will be 23456. As the given type is float,
*s will take the value as a whole from four bytes.
8 bits 8 bits
8 bits 8 bits 8 bits 8 bits
Notes:
 When the address of a variable is assigned to a pointer
variable, only the starting byte’s address will be stored.
According to the data type the no of bytes from the starting
bytes will be taken account.
 If the data type is not proper, pointer variable will not be
knowing till which byte the data is stored.
 The arithmetic operations performed on the pointer variable,
it must be applied such that it is able to retrieve the whole
value whether it is an integer or float or char etc.,
 When a pointer of certain base type is increased, it increases
its value in such a way that it points to next element of its
base type.
 If a pointer of certain base type is decremented, its value
decreases in such a way that it points to previous value of its
base type.
 Increment as well as decrement in fixed quanta of size of the
base type.
 Pointer value can be incremented or decremented only by
integer as it is holding the address.
Two forms of pointer arithmetic:
 Pointer + integer
 Pointer – pointer (Will dealt later while dealing with array and
pointers
Unary Pointer Arithmetic Operators:
Pointer variable ++;
 Adds sizeof(datatype) number of bytes to pointer, so that it
points to the next entry of the datatype.
 The no of bytes is determined by the data type of the value
the pointer variable is pointing to.
Pointer variable --;
 Subtracts sizeof(datatype) number of bytes to pointer, so that
it points to the next entry of the datatype.
 Example of incrementing the float type pointer.
23456 23457 23458 23459 23460 23461 23462
-------
4 bytes form the float value Incrementing the pointer will
make the pointer to point to starting of next 4 bytes
main()
{
float a = 2.3, *b=&a;
printf(“Address of á’ stored in b:”, b);
printf(“Value stored in a:”, *b);
printf(“Incrementing the pointer variable:
”, ++b);
printf(“Value retrieved after
incrementing:”, *b);
}
Output:
Address of á’ stored in b: 23456
Value stored in a: 2.3
Incrementing the pointer variable: 23460
Value retrieved after incrementing:
// Not known. Will be retrieved at the
execution time.
8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits
main()
{
int *ptrn;
int a;
float *ptrflt;
float b;
ptrn=&a;
ptrflt=&b;
ptrn++; //increments by sizeof(int) (2 bytes)
ptrflt--; //increments by sizeof(long) (4 bytes)
}
Arithmetic Operations between a pointer and an integer:
Pointer + integer
Pointer Variable + n is valid, if n is an integer.
The result is the following:
Pointer Variable + (n*sizeof(data type of the value pointer points
to))
It advances the pointer by n number of size of data type.
Pointer Variable - n is similar.
The result will be:
Pointer Variable - (n*sizeof(data type of the value pointer points
to))
It decrements the pointer by n number of size of data type.
Eg:
float h=4.6, *ptrf;
ptrf=&h;
If h is stored at address 23454, then ptrf will be having the address
value 23454 stored in it.
Pointer Increment Value Incremented Pointing address Value retrieved
ptrf - 23454 4.6
ptrf+1
// ptrf++
ptrf+(1*sizeof(float)
)
23458 Points to the next four
bytes starting from 23458
ptrf+2 ptrf+(2*sizeof(float)
)
23462 Points to the next four
bytes starting from 23462
ptrf+3 Ptrf+(3*sizeof(float)
)
23466 Points to the next four
bytes starting from 23466
Notes:
 Arithmetic operations addition (or) subtraction of an integer value
can be done on a pointer.
 Multiplication, Division, Modulus by an integer cannot be done on
a pointer. This will give the error Illegal use of a pointer in function
main.
 Addition of two pointers cannot be done. This will give the error
Invalid pointer addition.
 Pointer variable can be compared only with another pointer.
 Comparison can be done using <, >, ==, <= and >= operators.
 When two pointers are compared, actually the address they are
holding is compared.
 Using ‘==’ operator on pointers, will check whether both pointers
being compared are pointing to the same address or not
 If pointer a and pointer b are holding the same address, then
a==b will be true. Otherwise false.
 The use arithmetic operations and the comparisons will be having
wide space when the pointer is applied to array.
 A pointer can be checked whether it is pointing to NULL using the
‘==’ (Equal to) operator
int *p=NULL;
if(p==0 ) will return true.
Valid Comparisons:
 Comparing same type pointers.
 Comparing pointers pointing to same location.
 Comparison can be done to check for NULL pointer
Invalid Comparisons:
Comparing pointer and a normal variable. // Compiler Error
Comparing different type of pointers. Such as comparing char
pointer with int or some other type of pointer. // Warning

More Related Content

What's hot

Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanMohammadSalman129
 
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in CShivanshuVerma11
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++Muhammad Hammad Waseem
 
VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3YOGESH SINGH
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in CUri Dekel
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_castsAbed Bukhari
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Jayanshu Gundaniya
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to PointersMuhammad Hammad Waseem
 

What's hot (17)

Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Fundamentals of Pointers in C
Fundamentals of Pointers in CFundamentals of Pointers in C
Fundamentals of Pointers in C
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
 
VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3VIT351 Software Development VI Unit3
VIT351 Software Development VI Unit3
 
Introduction to pointers and memory management in C
Introduction to pointers and memory management in CIntroduction to pointers and memory management in C
Introduction to pointers and memory management in C
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
Lect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer AbbasLect 8(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer Abbas
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Pointers In C
Pointers In CPointers In C
Pointers In C
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
Computer Science:Pointers in C
Computer Science:Pointers in CComputer Science:Pointers in C
Computer Science:Pointers in C
 
Pointers
PointersPointers
Pointers
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_casts
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Ankita sharma focp
Ankita sharma focpAnkita sharma focp
Ankita sharma focp
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 

Similar to Pointers operation day2 (20)

Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
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
 
FYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptxFYBSC(CS)_UNIT-1_Pointers in C.pptx
FYBSC(CS)_UNIT-1_Pointers in C.pptx
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
C programming session8
C programming  session8C programming  session8
C programming session8
 
Pointers
PointersPointers
Pointers
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
Pointers
PointersPointers
Pointers
 
C pointer
C pointerC pointer
C pointer
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Pointers
PointersPointers
Pointers
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 

Recently uploaded

the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 

Recently uploaded (20)

the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
(TARA) Talegaon Dabhade Call Girls Just Call 7001035870 [ Cash on Delivery ] ...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 

Pointers operation day2

  • 2. Type Casting: Converting the data type of one variable into another data type. Syntax: (data type to be converted to) variable; Eg: float a = 2.3; int b; b=(int)a; Types: Implicit Conversion: int float double (small no of bytes higher no of bytes) Explicit Conversion: double float int (higher no of bytes small no of bytes)
  • 3. Converting pointer from one type to another:  Pointer of one type cannot be implicitly converted from one type to another but can be explicitly converted using type casting.  In such a conversion a pointer always assumes that it is point to a object of its type but reality may differ. Syntax: (data type to be converted to *) variable; int *p; (char *)p; Normal variable cannot be converted to pointer variable using type casting. Pointer variable of one type can be type casted to another type.
  • 4. Notes:  Type conversion is a powerful feature but yet it may difficult to remove bugs and crashes and should be used with uttermost vigilance.  It may also lead to unexpected and unreliable results but program would compile successfully.  While typecasting one pointer to another because even after type casting the pointer can point to anything but it will still think it is pointing to something of it declared type and have properties of the original type.  void pointer can be used for this purpose. As the void pointer doesn’t point to any type of data, it can be type casted to any type, and results in no error.
  • 5.  Example program to convert the pointer from pointing to int to char data type. main() { int i = 10; char *p1; int *p2; p2 = &i; p1 = (char *) p2; // Type Casting and Pointer Conversion printf (" *p1 = %c And *p2 = %d", *p1,*p2); // Output will be depending upon the compiler. }
  • 6.  Example program to convert the pointer from pointing to void to char data type. main() { int i = 10; int *p1; void *p2; p2 = &i; p1 = (int *) p2; // Type Casting and Pointer Conversion printf (" *p1 = %d And *p2 = %d", *p1,*p2); } Output: *p1 = 10 And *p2 = 10
  • 7. int a=13; 2 bytes as a whole form the 65345 65346 integer float q=2.3; float *s; s=&q; *s will give the value 2.3 4 bytes form the 23456 25457 25458 25459 float value Pointer variable ‘s’ will be assigned with the address of q. The address of q assigned will be 23456. As the given type is float, *s will take the value as a whole from four bytes. 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits
  • 8. Notes:  When the address of a variable is assigned to a pointer variable, only the starting byte’s address will be stored. According to the data type the no of bytes from the starting bytes will be taken account.  If the data type is not proper, pointer variable will not be knowing till which byte the data is stored.  The arithmetic operations performed on the pointer variable, it must be applied such that it is able to retrieve the whole value whether it is an integer or float or char etc.,
  • 9.  When a pointer of certain base type is increased, it increases its value in such a way that it points to next element of its base type.  If a pointer of certain base type is decremented, its value decreases in such a way that it points to previous value of its base type.  Increment as well as decrement in fixed quanta of size of the base type.  Pointer value can be incremented or decremented only by integer as it is holding the address. Two forms of pointer arithmetic:  Pointer + integer  Pointer – pointer (Will dealt later while dealing with array and pointers
  • 10. Unary Pointer Arithmetic Operators: Pointer variable ++;  Adds sizeof(datatype) number of bytes to pointer, so that it points to the next entry of the datatype.  The no of bytes is determined by the data type of the value the pointer variable is pointing to. Pointer variable --;  Subtracts sizeof(datatype) number of bytes to pointer, so that it points to the next entry of the datatype.
  • 11.  Example of incrementing the float type pointer. 23456 23457 23458 23459 23460 23461 23462 ------- 4 bytes form the float value Incrementing the pointer will make the pointer to point to starting of next 4 bytes main() { float a = 2.3, *b=&a; printf(“Address of á’ stored in b:”, b); printf(“Value stored in a:”, *b); printf(“Incrementing the pointer variable: ”, ++b); printf(“Value retrieved after incrementing:”, *b); } Output: Address of á’ stored in b: 23456 Value stored in a: 2.3 Incrementing the pointer variable: 23460 Value retrieved after incrementing: // Not known. Will be retrieved at the execution time. 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits 8 bits
  • 12. main() { int *ptrn; int a; float *ptrflt; float b; ptrn=&a; ptrflt=&b; ptrn++; //increments by sizeof(int) (2 bytes) ptrflt--; //increments by sizeof(long) (4 bytes) }
  • 13. Arithmetic Operations between a pointer and an integer: Pointer + integer Pointer Variable + n is valid, if n is an integer. The result is the following: Pointer Variable + (n*sizeof(data type of the value pointer points to)) It advances the pointer by n number of size of data type. Pointer Variable - n is similar. The result will be: Pointer Variable - (n*sizeof(data type of the value pointer points to)) It decrements the pointer by n number of size of data type.
  • 14. Eg: float h=4.6, *ptrf; ptrf=&h; If h is stored at address 23454, then ptrf will be having the address value 23454 stored in it. Pointer Increment Value Incremented Pointing address Value retrieved ptrf - 23454 4.6 ptrf+1 // ptrf++ ptrf+(1*sizeof(float) ) 23458 Points to the next four bytes starting from 23458 ptrf+2 ptrf+(2*sizeof(float) ) 23462 Points to the next four bytes starting from 23462 ptrf+3 Ptrf+(3*sizeof(float) ) 23466 Points to the next four bytes starting from 23466
  • 15. Notes:  Arithmetic operations addition (or) subtraction of an integer value can be done on a pointer.  Multiplication, Division, Modulus by an integer cannot be done on a pointer. This will give the error Illegal use of a pointer in function main.  Addition of two pointers cannot be done. This will give the error Invalid pointer addition.
  • 16.  Pointer variable can be compared only with another pointer.  Comparison can be done using <, >, ==, <= and >= operators.  When two pointers are compared, actually the address they are holding is compared.  Using ‘==’ operator on pointers, will check whether both pointers being compared are pointing to the same address or not  If pointer a and pointer b are holding the same address, then a==b will be true. Otherwise false.  The use arithmetic operations and the comparisons will be having wide space when the pointer is applied to array.  A pointer can be checked whether it is pointing to NULL using the ‘==’ (Equal to) operator int *p=NULL; if(p==0 ) will return true.
  • 17. Valid Comparisons:  Comparing same type pointers.  Comparing pointers pointing to same location.  Comparison can be done to check for NULL pointer Invalid Comparisons: Comparing pointer and a normal variable. // Compiler Error Comparing different type of pointers. Such as comparing char pointer with int or some other type of pointer. // Warning