Sei sulla pagina 1di 31

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 arithmetic
Teaching Aids : PPT, Animations
CM304.70 1
Recap

In the previous lesson, you have learnt..

• Understand Pointer concept.

• Understand Addressing operators.

• Understand dereffencing operators.

CM304.70 2
Objective

On completion of this period, you would be able


to
• Declare a pointer.

• Assign value to a pointer.

• Initialize a pointer.

• Understand pointer arithmetic.


CM304.70 3
Declaring a pointer

Syntax :
Type-name *pointer name;

Example :
int *ptr;
• means that ptr is a pointer variable.

• ptr can be used to point to variables of type int.

CM304.70 4
Declaring a variable

• The operator * does not distribute to all variable


names in a declaration.
Example :
int *p1,p2;

p1 is a pointer variable, which can point to


integer data.
p2 is an integer variable.

CM304.70 5
Pointer declaration

• Each pointer must be declared with the *


prefixed to the name.
Example :
int *p1,*p2;

declares p1, p2 as pointer variables that can


point to integer data.

CM304.70 6
Pointer assignment
• A pointer variable can be used on the right
hand side of an assignment statement to
assign its value to another pointer.
Example :
#include<stdio.h>
main()
{
int a,*p1,*p2;
p1=&a;
p2=p1;
printf(“%d%d”,p1,p2);}

CM304.70 7
Initializing a pointer
• Pointers should be initialized when they are
declared OR in an assignment statement.

• A pointer may be initialized to 0, null or an


address.

• A pointer with the value null points to nothing.

• Initializing a pointer to 0 is same as initializing it


to null.
CM304.70 8
Initializing a pointer

Example:
int p1,*ptr=&p1;

• Here we declared two variables p1 and a pointer


variable ptr.

• Pointer variable ptr is initialized with the address


of p1.

CM304.70 9
Initializing a pointer Contd..
Example :
#include<stdio.h>
main()
{
int *a;
int b=786;
a=&b;
printf(“%d”,*a);
}

OUTPUT : 786
CM304.70 10
Initializing a
Contd..
pointer
• In the above program we declared two
variables ‘a’(pointer) and ‘b’.

• Variable ‘b’ is initialized to 786.

• Pointer variable ‘a’ is initialized to the address


of ‘b’.

• * operator returns the value stored at that


address. So *a returns 786.
CM304.70 11
Pointer arithmetic

• As in case of variables arithmetic operations


can be performed on pointers.

• Addition and subtraction operations are


possible on pointers.

• Postfix, prefix, increment, decrement


operations are also possible on pointers.

CM304.70 12
Pointer arithmetic
Contd..
• In variables postfix, prefix, increment, or
decrement means addition or subtraction by
one.

• In pointer variables they mean addition or


subtraction of bytes that pointer data type
holds, with the value that the pointer variable
contains.

CM304.70 13
Operations on pointer variables
• Addition of a number to a pointer variable.
Example:
p++;
k=k+3; /* p,k are pointer variables */

• Subtraction of a number from a pointer


variable.
Example :
p--;
CM304.70 14
Operations on pointer variables
Contd..
• Subtraction of one pointer from another pointer
Example:
p-k;

• Comparison of two pointer


Example :
if(p==k)
printf(“ both are pointing to same location”);

CM304.70 15
Operations not allowed on pointers

Contd..
• Addition of two pointers.

• Multiplication of a pointer with a constant.

• Division of a pointer with a constant.

CM304.70 16
Example on pointer arithmetic
int var,*ptr;

ptr=&var;

var=1500;

• Let var be an integer variable having a value


1500 and stored at address 1000.

• ptr has the value 1000.


CM304.70 17
Example on pointer arithmetic
Contd..
• After the expression ptr++,
ptr contains 1002 and not 1001.

• Because each time ptr is incremented, it will


point to the next integer and since integers
are two bytes long, ptr will increment by 2 and
the same is true for decrements.

CM304.70 18
Some more examples on pointer arithmetic

• ++ptr or ptr++ ----- points to next integer after


var

• --ptr or ptr-- ----- points to integer previous to


var

• ptr+I ----- points to the ith integer after


var

CM304.70 19
Some more examples on pointer arithmetic

• ptr-I ---- points to the ith integer


before var.
• ++(*ptr) or (*ptr)++ ---- will increment var by one.
• *ptr++ ---- will fetch the value of the
next integer after var.

CM304.70 20
Pointer arithmetic
Contd..

• Each time a pointer is incremented, it points


to the memory location of the next element of
it’s base type.

• Each time it is decremented, it points to the


location of the previous element.

• With pointers to character data type this


appears normal because characters occupy 1
byte per character.
CM304.70 21
Pointer arithmetic
Contd..

• Pointers cannot be multiplied or divided.

• Float, double type cannot be added or


subtracted to or from pointers.

CM304.70 22
Summary
In this class, we have discussed about…
• Each pointer must be declared with the * prefixed to the
name.
• A pointer variable can be used on the right hand side of
an assignment statement to assign it’s value to another
pointer.
• Pointers should be initialized when they are declared or
in an assignment statement.
• Addition and subtraction operations are possible on
pointers.

CM304.70 23
Quiz

1. int *p1,p2 declares

a) p1,p2 as pointer variables

b) p1 as pointer variable and p2 as integer variable

c) p1,p2 as integer variables

CM304.70 24
Quiz

1. int *p1,p2 declares

a) p1,p2 as pointer variables

b) p1 as pointer variable and p2 as integer variable

c) p1,p2 as integer variables

CM304.70 25
Quiz
2. Each time when a pointer variable is
incremented it points to the memory location of
it’s base type
a)True

b) False

c) None

CM304.70 26
Quiz
2. Each time when a pointer variable is
incremented it points to the memory location of
it’s base type
a)True

b) False

c) None

CM304.70 27
Quiz

3. Pointers can be multiplied or divided

a) True

b) False

c) Not known

CM304.70 28
Quiz

3. Pointers can be multiplied or divided

a) True

b) False

c) Not known

CM304.70 29
Assignment

1) Explain how to declare ,initialize,assign


pointers.

3) List the arithmetic operations that can be


performed on pointers and the operations that
cannot be performed.

CM304.70 30
Frequently Asked Questions

1. Explain pointer arithmetic with examples.


(Apr 2002)
2. Define a pointer. Explain how to declare,
assign, initialize a pointer with syntax and
examples. (Mar/Apr 2007)

CM304.70 31