Vous êtes sur la page 1sur 18

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name : Murali Krishna Chintala
Designation : Lecturer in CME
Branch : Computer Engineering
Institute : SUVR & SR GPW, Ethamukkala
Year/Semester : III Semester
Subject : UNIX & C
Subject Code : CM – 304
Topic : Basics of Pointers
Duration : 50 Min
Sub Topic : Pointer Comparison and Conversion
Teaching Aids : PPT, Animations

CM304.71 1
Objective:
On completion of this period, you would be able to
know…
• Understand the precedence of addressing and
De-referencing operators.

• Understand pointer comparison.

• Know pointer conversion.

CM304.71 2
Precedence of address and De-referencing
operators
• Multiplication sign and De-referencing sign
are the same.

• Bit-wise AND and address of sign are the


same.

• Though they have the same sign both & and *


have higher precedence than all other
arithmetic operators except the unary ‘ - ‘
with which their priority is equal.

• In such case the priority is from left to right.


CM304.71 3
Pointer comparison

• Two pointers can be compared in a relational


expression.

• Two pointers can be compared only when both


are pointing to variables of same type.

• If ptr-a and ptr-b are two pointers which point to


data elements a and b, examples showing the
pointer comparison are given below.
CM304.71 4
Example for Pointer comparison

ptr-a < ptr-b ---- evaluates true provided a is

stored before b.

ptr-a > ptr-b ---- evaluates true provided a is


stored after b.

ptr-a <= ptr-b --- evaluates true provided a is


stored before b OR ptr-a and
ptr-b point to the same location.
CM304.71 5
Example for Pointer comparison
• ptr-a >= ptr-b -- evaluates true provided a is stored
after b OR ptr-a and ptr-b point to
the same location.

• ptr-a == ptr-b -- evaluates true provided both


pointers ptr-a and ptr-b point
different data of the same type.

• ptr-a == null --- evaluates true provided ptr-a is


assigned null value.

CM304.71 6
Pointer comparison

• If ptr-begin and ptr-end point to the numbers of


the same array then ptr-end – ptr-begin gives
the difference in bytes between the storage
locations to which they point.

CM304.71 7
Pointer conversion
Contd…

• A pointer may be converted to an integer type


large enough to hold it.

• An object of integer type may be explicitly


converted to a pointer.

• A pointer to one type may be converted to a


pointer to another type.

CM304.71 8
Pointer conversion
Contd..

• It is guaranteed that a pointer to an object may be


converted to a pointer to another object whose
type requires less or equal storage and back again
without change.

CM304.71 9
Pointer conversion
Contd..

• A pointer to one type can be converted to a


pointer of another type by using an explicit ‘cast’.

• The cast (T*) converts it’s operand into a pointer


to an object of type T.

CM304.71 10
Pointer conversion
Contd..

Example :
A double pointer dp can be converted
into an integer pointer ip by writing

ip= (int *)dp;


and back to a double pointer by writing

dp= (double *)ip;

CM304.71 11
Summary
In this class, we have learnt about..

• Precedence of addressing and de-referencing


operators is more over all other arithmetic
operations except unary ‘ – ‘.

• Two pointers can be compared if both pointers


are pointing to variables of the same type.

• A pointer of one type can be converted to a


pointer of another type.

CM304.71 12
Quiz

1. A pointer of one type can be converted into a


pointer of another type

a) True

b) False

c) None

CM304.71 13
Quiz

1. A pointer of one type can be converted into a


pointer of another type

a) True

b) False

c) None

CM304.71 14
Quiz

2. Precedence of addressing and de-


referencing operators is

a) Higher than arithmetic operators

b) Lower than arithmetic operators

c) Equal to arithmetic operators

CM304.71 15
Quiz

2. Precedence of addressing and de-


referencing operators is

a) Higher than arithmetic operators

b) Lower than arithmetic operators

c) Equal to arithmetic operators

CM304.71 16
Assignment

 Write the precedence of operators including


de-referencing and addressing operators?

 Explain pointer comparison with example?

CM304.71 17
Frequently Asked Questions

1) Explain pointer conversion with examples.


.
2) Explain pointer comparison with examples.

CM304.71 18

Vous aimerez peut-être aussi