Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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
CM304.70 2
Objective
• Initialize a pointer.
Syntax :
Type-name *pointer name;
Example :
int *ptr;
• means that ptr is a pointer variable.
CM304.70 4
Declaring a variable
CM304.70 5
Pointer declaration
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.
Example:
int p1,*ptr=&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’.
CM304.70 12
Pointer arithmetic
Contd..
• In variables postfix, prefix, increment, or
decrement means addition or subtraction by
one.
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 */
CM304.70 15
Operations not allowed on pointers
Contd..
• Addition of two pointers.
CM304.70 16
Example on pointer arithmetic
int var,*ptr;
ptr=&var;
var=1500;
CM304.70 18
Some more examples on pointer arithmetic
CM304.70 19
Some more examples on pointer arithmetic
CM304.70 20
Pointer arithmetic
Contd..
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
CM304.70 24
Quiz
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
a) True
b) False
c) Not known
CM304.70 28
Quiz
a) True
b) False
c) Not known
CM304.70 29
Assignment
CM304.70 30
Frequently Asked Questions
CM304.70 31