Pointer
Pointer
1
Introduction
2
Basic Concept
3
Contd.
4
Contd.
6
Address vs. Value
7
Values vs Locations
value
1024: 32
x
address name New Type : Pointer
8
Pointers
int *ptr;
ptr doesn’t actually point to anything yet. We can
either:
– make it point to something that already exists, or
– allocate room in memory for something new that it will
point to… (next time)
9
Pointer
10
Pointers
11
Pointer Usage Example
0xcafe 0000
0xbeef 0000
0x0000 0004
0x0000 0000
12
Pointer Usage Example
0x0000 0004
0x0000 0000
13
Pointer Usage Example
0x0000 0004
0x0000 0000
14
Pointer Usage Example
0x0000 0004
0x0000 0000
15
Pointer Usage Example
0x0000 0004
0x0000 0000
16
Accessing the Address of a Variable
• Example:
p = &xyz;
– The address of xyz (1380) is assigned to
p.
• The ‘&’ operator can be used only with a simple variable or an
array element.
&distance
&x[0]
&x[i-2]
17
Contd.
int arr[20];
:
&arr;
• Pointing at array
name.
&(a+b)
• Pointing at
expression.
18
Example
#include <stdio.h>
main()
{
int
a; float
double
b, c;
char d;
ch;
a = 10; b = 2.5; c = 12.36; d = ch = ‘A’;
12345.66;
printf (“%d is stored in location %u \n”, a, &a) ;
printf (“%f is stored in location %u \n”, b, &b) ;
printf (“%f is stored in location %u \n”, c, &c) ;
printf
printf (“%ld
(“%c isisstored
storedin
inlocation
location%u
%u\n”,
\n”, d,
ch, &d) ;;
&ch)
}
19
Output:
20
Pointer Declarations
• General form:
data_type *pointer_name;
21
Contd.
• Example:
int *count;
float *speed;
22
Things to Remember
float
x;
int *p; will result in erroneous output
:
p = &x;
int *count;
:
count = 1268;
23
Accessing a Variable Through its Pointer
int a,
b;
Equivalent to b = a;
int *p;
:
p = &a;
b =
*p;
24
Example 1
#include <stdio.h>
main()
{ Equivalent
int a,
b;
int c =
5;
int *p;
(c + 5) ;
p
a = &c;= 4 *
b = 4 * (*p + 5) ;
printf (“a=%d b=%d \n”, a, b);
} a=40 b=40
25
Example 2
#include <stdio.h>
main()
{
int x, y;
int *ptr;
x = 10 ;
ptr = &x ;
y = *ptr ;
printf (“%d is stored in location %u \n”, x, &x) ;
printf (“%d is stored in location %u \n”, *&x, &x) ;
printf (“%d is stored in location %u \n”, *ptr, ptr);
printf (“%d is stored in location %u \n”, y,
&*ptr) ;
printf (“%u is stored in location %u \n”, ptr, &ptr) ;
printf (“%d is stored in location %u \n”, y, &y) ;
*ptr = 25;
printf (“\nNow x = %d \n”, x);
} 26
Address of x: 3221224908
Address of y: 3221224904
Address of ptr: 3221224900
Output:
Now x = 25
27
Pointer Expressions
28
Contd.
29
Contd.
30
Scale Factor
32
• Note:
– The exact scale factor may vary from one machine to another.
– Can be found out using the sizeof function.
– Syntax:
sizeof (data_type)
33
Example: to find the scale factors
#include <stdio.h>
main()
{
printf (“No. of bytes occupied by int is %d \n”, sizeof(int));
printf (“No. of bytes occupied by float is %d \n”, sizeof(float));
printf (“No. of bytes occupied by double is %d \n”, sizeof(double));
printf (“No. of bytes occupied by char is %d \n”, sizeof(char));
}
Output:
34
Passing Pointers to a Function
35
Example: passing arguments by value
#include <stdio.h>
main()
{
int a, b;
a = 5; b = 20;
swap (a, b); Output
printf (“\n a=%d, b=%d”, a, b);
} a=5, b=20
36
Example: passing arguments by reference
#include <stdio.h>
main()
{
int a, b;
a = 5; b = 20;
swap (&a, &b); Output
printf (“\n a=%d, b=%d”, a, b); a=20, b=5
}
37
Pointers and Arrays
38
Example
39
Contd.
41
Pointers and Structures
• You may recall that the name of an array stands for the
address of its zero-th element.
– Also true for the names of arrays of structure variables.
•
Consider the declaration:
struct stud {
int roll;
char
dept_code[25];
float cgpa;
} class[100], *ptr ;
42
– The name class represents the address of the zero-
th element of the structure array.
– ptr is a pointer to data objects of the type struct
stud.
• The assignment
ptr = class;
will assign the address of class[0] to
ptr.
• When the pointer ptr is incremented by one (ptr+
+) :
– The value of ptr is actually increased by sizeof(stud).
– It is made to point to the next record.
43
• Once ptr points to a structure variable, the
members can be accessed as:
ptr –> roll;
ptr –> dept_code;
ptr –> cgpa;
44
A Warning
45
Structures and Functions
46
Example: complex number addition
#include <stdio.h> struct complex add (x, y)
struct complex { struct complex x, y;
f {
l
struct complex t;
o
a
t t.re = x.re + y.re ;
main() t.im = x.im +
{ r y.im ; return (t) ;
struct complexe a, b, c; }
;
scanf (“%f %f”, &a.re, &a.im);
f
scanf (“%f %f”, &b.re, &b.im);
l
c add (a, b)o ;
(“\n %fa%f”, c,re, c.im);
} = t
printf
i
m
;
}; 47
Example: Alternative way using pointers
#include <stdio.h>
struct complex {
f
l void add (x, y, t)
o
struct complex *x, *y, *t;
a
t {
main() t->re = x->re + y->re;
{ r t->im = x->im + y-
struct complex e a, b, c; } >im;
;
scanf (“%f %f”, &a.re, &a.im);
f
(“%f %f”, &b.re, &b.im);
l
scanf
add (&a, &b, &c) o;
printf (“\n %f %f”,
a c,re, c.im);
} t
i
m
;
}; 48
Linked List :: Basic Concepts
49
Contd.
50
Contd.
• Each structure of the list is called a node, and
consists of two fields:
– One containing the item.
– The other containing the address of the next item in the
list.
• The data items comprising a linked list need not
be contiguous in memory.
– They are ordered by logical links that are stored as part
of the data in the structure itself.
– The link is a pointer to another structure of the same
type.
51
Contd.
• next
struct node_name
{
type
member1; type
member2;
………
struct
node_name
*next;
}
53
Illustration
roll; char
name[30]; int
age;
struct stud
*next;
}
• Also assume that the list consists of three nodes n1, n2
and n3.
struct stud n1, n2, n3;
54
Contd.
roll
name
age
next
n1 n2 n3
55
Example
#include <stdio.h>
struct stud
{
int roll;
char
name[30]; int
age;
struct
stud
main()*next;
{ }
struct stud n1, n2, n3;
*p;
struct stud
scanf (“%d %s %d”, &n1.roll, n1.name, &n1.age);
scanf (“%d %s %d”, &n2.roll, n2.name,
&n2.age); scanf (“%d %s %d”, &n3.roll,
n3.name, &n3.age);
56
n1.next = &n2 ;
n2.next = &n3 ;
n3.next = NULL ;
p = n1 ; /* point to 1st
element */ while (p !=
NULL)
{
printf (“\n %d %s %d”,
p->roll, p->name, p->age);
p = p->next;
}
}
57