You are on page 1of 28

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 : 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 Addressing operator.
 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.

 The address of the data item is the address of its first


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 x=10; Starting address


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

1020 y 1000 x
Value
1000 10 of x

Address 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 return more than one value from a function.

 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.

 To communicate information about memory.

CM304.69 9
Uses of Pointers
Contd..

 Pointers are used for saving memory.

 With pointers,data manipulation is done with


addresses,so the execution time is less.

 Pointers provide us dynamic memory allocation.

CM304.69 10
Address & De-referencing operators
x
int x=10; 10
p
Garbage address
int *p ;
y
Garbage values
int y;

CM304.69 11
Contd..
Address & De-referencing operators

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

y=*p;
y
10
CM304.69 12
Address and De-referencing operators

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


manipulating data using pointers.

 ‘&’ is known as the addressing operator.

 ‘*’ is known as the De-referencing operator or Indirection


operator.

CM304.69 13
Address and De-referencing operators
Contd..
 ‘&’ is read as “address off”.

 When used with a variable, ‘&’ returns the address of


the variable.

Example:
a=&count;

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

CM304.69 14
Address and De-referencing operators

 ‘*’ is read as “the value at address”


 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
Usage Of Address Operator
Example:

#include<stdio.h>
main()
{
int x=100;
printf(“address of x is %u”,&x);
}
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(“address of x is %u”,&x);
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
Differences between addressing and
De-referencing operators
Address operator De-referencing operator

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
Differences between addressing and
De-referencing operators
Address operator De-referencing operator

4. Returns the memory 4. Returns the value of the


address of its operand. variable at the address.

5. Unary operator. 5. Unary operator.

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

2.The operator & means

a. The address off


b. Value at address
c. None

CM304.69 24
Quiz

2.The operator & means

a. The address off


b. Value at address
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
Frequently Asked Questions

1..Write the differences between the addressing


and de-referencing operators. (Mar/Apr 2007)

2.Explain pointer concept with an example.

CM304.69 28

You might also like