Module-1 2
Module-1 2
Module-1 2
IOT
DATA STRUCTURES
MODULE-1: POINTERS
1
Pointers
▶ POINTER ARITHMETIC
2
COMPUTER MEMORY REVISITED
▶ Computers store data in memory slots
▶ Each slot has an unique address
▶ Variables store their values like this:
Addr Content Addr Content Addr Content Addr Content
1000 i: 37 1001 j: 46 1002 k: 58 1003 m: 74
1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘c’ 1007 a[3]: ‘\0’
1008 ptr: 1001 1009 … 1010 1011
1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘z’ 1007 a[3]: ‘\0’
1008 ptr: 1001 1009 … 1010 1011
Pointers
▶ Pointer is a variable which holds the address of another variable of same
data type.
▶ It refers to a memory location
4
Why do we need Pointers ?
▶ It is used in some circumstances in
C.
Remember this?
scanf(“%d”, &i);
Why actually ptr is?
▶ ptr is a variable storing an address
▶ ptr is NOT storing the actual value of
ptr address of i
i
int i = 5;
int *ptr;
i 5
ptr = &i;
Output:
printf(“i = %d\n”, i);
i = 5 value of ptr5 =
printf(“*ptr = %d\n”, *ptr);
*ptr = 5 address of i
printf(“ptr = %p\n”, ptr); in memory
ptr = effff5e0
Benefits of using Pointers
▶ Pointers are more efficient in handling Arrays and Structures.
6
Declaration of pointer variable
▶ General syntax:
datatype *pointer_name;
7
Initialization of pointer variable
▶ Pointer Initialization is the process of assigning address of
a variable to a pointer variable.
▶ Example:
#include <stdio.h>
void main( )
{
int a = 10;
int *ptr; //Pointer declaration
ptr = &a; // Pointer
} initialization
8
Initialization of pointer variable
▶ Example:
int i = 5;
int *ptr; /* declare a pointer variable */
ptr = &i; /* store address-of i to ptr */
printf(“*ptr = %d\n”, *ptr); /* refer to referee of ptr */
9
Null pointer
▶ If you are not sure about which variable's address to
assign to a pointer variable while declaration, it is
recommended to assign a NULL value to your pointer
variable. A pointer which is assigned a NULL value is
called a NULL pointer.
▶ Example:
#include <stdio.h>
int main( )
{
int *ptr = NULL;
return 0;
} 10
Pointers - Example program
#include <stdio.h>
void main()
{
int a = 10;
int *p; //Pointer declaration
p = &a; // Pointer initialization
12
Different Ways of assigning Pointers
(a) Pointer to Pointer (Double pointer)
(b) Pointer to Array
(c) Pointer to Functions
13
(a) Pointer to pointer (double pointer)
▶ When one pointer variable stores the address of another pointer
variable, it is known as Pointer to Pointer variable or Double Pointer.
▶ Syntax:
Data_type **pointer_name;
int **p1;
14
(a) Pointer to pointer (Example program)
#include <stdio.h>
int main()
{
int a = 10;
int *p1; //this can store the address of variable a
int **p2; // this can store the address of pointer variable p1 only.
p1 = &a;
p2 = &p1;
printf("Address of a = %d\n", &a);
printf("Address of p1 = %d\n", &p1);
printf("Address of p2 = %d\n\n",
&p2);
// below print statement will give the
address of 'a'
printf("Value at the address stored
by p2 = %d\n", *p2);
printf("Value at the address stored by p1 = %d\n\n", *p1); 15
printf("Value
return 0; of **p2 = %d\n", **p2); //read this *(*p2)
}
(b) Pointer to array
▶ We can use a pointer to point to an array, and then we can use that
pointer to access the array elements.
▶ Example:
int *p;
p = arr; (or) p = &arr[0]
16
(b) Pointer to array (Example program)
#include <stdio.h>
int main()
{
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p =
&a[0]
for (i = 0; i < 5; i++)
{
printf("%d\t", *p);
p++;
}
17
return 0;
}
(d) Pointer to functions
▶ Pointer as a function parameter is used to hold addresses of arguments
passed during function call. This is also known as call by reference.
18
(d) Pointer to functions(Example program)
#include <stdio.h>
void swap(int *a, int *b);
int main()
{ int m = 10, n = 20;
printf("m = %d\n", m);
printf("n = %d\n\n", n);
*a = *b; 19
int temp;
*b = temp;
temp = *a;
}
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i;
*ptr = -2;
20
An Illustration
int i = 5, j = 10;
int *ptr; /* declare a pointer-to-integer variable */
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable
*ptr = -2;
21
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr; /* declare a pointer-to-pointer-to-integer variable */
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable
*ptr = -2; pptr int ** integer pointer pointer variable
22
Double Indirection
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i; /* store address-of i to ptr */
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2; pptr int ** integer pointer pointer variable
*ptr int de-reference of ptr 23
5
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr =
&ptr; /*
store
address-of
ptr to pptr
*/
*ptr = 3;
**pptr = 7;
ptr = &j;
Introduction to Networking 24
**pptr = 9;
24
*pptr = &i;
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr =
&ptr; /*
store Data Table
address-of Name Type Description Value
ptr to pptr
*/ i int integer variable 5
*ptr = 3; j int integer variable 10
**pptr = 7; ptr int * integer pointer variable address of i
ptr = &j; pptr int ** integer pointer pointer variable address of ptr
**pptr = 9; *pptr int * de-reference of pptr 25
value of ptr
*pptr = &i; (address of i)
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 3
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2; pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 26
3
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2; pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-referen c e of pptr
2 7
7
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of j
*ptr = -2; pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 28
10
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of j
*ptr = -2; pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-referen ce of pptr
29
9
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2; pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr 30
value of ptr
(address of i)
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable -2
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2; pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 31
-2
Pointer Arithmetic
▶ What’s ptr + 1?
The next memory location!
▶ What’s ptr - 1?
The previous memory location!
▶ What’s ptr * 2 and ptr /
2?
Invalid operations!!!
32
Pointer Arithmetic and Array
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) ?
*ptr = 9.0;
a[3] float float array element (variable) ?
ptr = ptr - 3;
*ptr = 6.0; ptr float * float pointer variable
33
Pointer Arithmetic and Array
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) ?
*ptr = 9.0;
a[3] float float array element (variable) ?
ptr = ptr - 3;
*ptr = 6.0; ptr float * float pointer variable address of a[2]
34
Pointer Arithmetic and Array
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
a[3] float float array element (variable) ?
ptr = ptr - 3;
*ptr = 6.0; ptr float * float pointer variable address of a[2]
35
Pointer Arithmetic and Array
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
a[3] float float array element (variable) ?
ptr = ptr - 3;
*ptr = 6.0; ptr float * float pointer variable address of a[3]
36
Pointer Arithmetic and Array
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
a[3] float float array element (variable) 9.0
ptr = ptr - 3;
*ptr = 6.0; ptr float * float pointer variable address of a[3]
37
Pointer Arithmetic and Array
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
a[3] float float array element (variable) 9.0
ptr = ptr - 3;
*ptr = 6.0; ptr float * float pointer variable address of a[0]
38
Pointer Arithmetic and Array
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) 6.0
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
a[3] float float array element (variable) 9.0
ptr = ptr - 3;
*ptr = 6.0; ptr float * float pointer variable address of a[0]
39
Pointer Arithmetic and Array
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) 6.0
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
a[3] float float array element (variable) 9.0
ptr = ptr - 3;
*ptr = 6.0; ptr float * float pointer variable address of a[2]
40
Pointer Arithmetic and Array
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) 6.0
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 7.0
*ptr = 9.0;
a[3] float float array element (variable) 9.0
ptr = ptr - 3;
*ptr = 6.0; ptr float * float pointer variable address of a[2]
41
Summary
42