Anda di halaman 1dari 28

# DEPARTMENT OF TECHNICAL EDUCATION

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 : Address and De-referencing operators
Teaching Aids : PPT, Animations

CM304.69 1
Objective

## On completion of this period, you would be able to

know
 Understand the Pointer concept.
 Understand De-referencing operator.
 Understand the differences between the two
operators.

CM304.69 2
Recap

## In the previous lesson, you have leant about..

• Recursion

CM304.69 3
How to store variables in memory?

##  A data item is stored in memory in consecutive

memory locations depending on its type.

location.

##  This address can be stored in another data item and

manipulated in a program.

CM304.69 4
Pointer definition
 The address of a data item is called a pointer to the
data item.

##  A variable that holds an address is called a pointer

variable.
OR
A pointer is a variable that holds the memory address
of another variable.

CM304.69 5
Pointer

int *y;
Starting y=&x;
Pointer variable

1020 y 1000 x
Value
1000 10 of x

CM304.69 6
Pointer
Example
y x
1000 10

1020 1000

## ‘x’ is a variable with value 10 and is stored at

location 1000 and y is another variable which
stores the address 1000 at location 1020, since
variable y holds the address of variable x we say
that y points to x.

CM304.69 7
Uses of Pointers

##  To pass arrays and strings more conveniently from one

function to another.

##  To manipulate arrays more easily by moving pointers

to them instead of moving the arrays themselves.

CM304.69 8
Uses of Pointers
Contd..

##  To create complex data structures, such as linked lists

and binary trees, where one data structure must contain
references to another data structure.

CM304.69 9
Uses of Pointers
Contd..

##  With pointers,data manipulation is done with

addresses,so the execution time is less.

##  Pointers provide us dynamic memory allocation.

CM304.69 10
x
int x=10; 10
p
int *p ;
y
Garbage values
int y;

CM304.69 11
Contd..

p=&x p=&x
1020 p 1000 x 1020 p 1000 x
1000 10 1000 10

y=*p;
y
10
CM304.69 12

##  C provides two unary operators ‘&’ and ‘*’ for

manipulating data using pointers.

operator.

CM304.69 13
Contd..

the variable.

Example:
a=&count;

##  The address of the variable count is placed in a.

CM304.69 14

 When used with a variable, ‘*’ returns the value
of the variable
 Example :-
 a=&count;
 c=*a; places the value of count in c;

CM304.69 15
Example:

#include<stdio.h>
main()
{
int x=100;
}
Note:
Since addresses are unsigned integers %u is used.
Output:- Address of the variable x is printed

CM304.69 16
Usage of De-referencing operator
Example:
#include<stdio.h>
main()
{
int x=100;
printf(“value of x is &d”,x);
printf(“value of x is %u”,*(&x));
}

CM304.69 17
Usage of De-referencing operator Contd..

Output:
 Address of x is 324518943
 value of x is 100
 value of x is 100
 &x gives the address of the variable x

##  The expression *(&x) gives the value of x

CM304.69 18
De-referencing operators

## 1. Address operator is ‘&’ 1. De-referencing operator is

‘*’.
2. General syntax is 2. General syntax is
&variable name; *variable name;
ex: &x; ex: *x;
3. Used to initialize 3. Used to declare pointer
pointer variable. variable.

CM304.69 19
De-referencing operators

CM304.69 20
Summary

## In this class, we have learnt about..

 A pointer is a variable that holds the memory address of
another variable.
 ‘&’ is the address operator and ‘*’ is the de-referencing
operator.
 Pointers can be used to return more than one value from
a function.
 Pointers provide Dynamic memory allocation.
 & is used to initialize the variable and * is used to declare
the variable.

CM304.69 21
Quiz
1.Variable which holds the address of another
variable is

a. function
b. pointer
c. array

CM304.69 22
Quiz
1.Variable which holds the address of another
variable is

a. function
b. pointer
c. array

CM304.69 23
Quiz

c. None

CM304.69 24
Quiz

c. None

CM304.69 25
Quiz

## 3.The operators &,* are

a) Unary operators
b) Arithmetic operators
c) Logical operators

CM304.69 26
Quiz

## 3.The operators &,* are

a) Unary operators
b) Arithmetic operators
c) Logical operators

CM304.69 27