Module-1 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 42

Department of Computer Science & Engineering

IOT

DATA STRUCTURES

MODULE-1: POINTERS

1
Pointers

▶ POINTERS – DEFINITION – INITIALIZATION

▶ DIFFERENT WAYS OF ASSIGNING 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

▶ Altering the value of a variable is indeed changing the content of


the
memory
Addr▶ e.g. i = 40; a[2] Addr
Content = ‘z’; Content Addr Content Addr Content
1000 i: 40 1001 j: 46 1002 k: 58 1003 m: 74 3

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.

▶ Pointers allow references to function and thereby helps in passing of


function as arguments to other functions.

▶ It reduces length of the program and its execution time as well.

▶ It allows C language to support Dynamic Memory management.

6
Declaration of pointer variable
▶ General syntax:

datatype *pointer_name;

▶ Pointer works with all data types:

int *ip; //pointer to integer variable


float *fp; //pointer to float variable
double*dp; //pointer to double variable
char *cp; //pointer to char variable

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

printf("p = %d\n", p); // prints the address of 'a'


printf("&p = %d\n", &p); // prints the address of 'p'
printf("*p = %d\n", *p); // prints value of that address
printf("*&p = %d\n", *&p); // prints value of that address
printf("a = %d\n", a); // prints value of a
printf("&a = %d\n", &a); // prints address of a
return 0;
} 11
Points to remember while using pointers
▶ While declaring/initializing the pointer variable, indicates that the
*
variable is a pointer.

▶ The address of any variable is given by preceding the variable name


with symbol Ampersand ’&’.

▶ The pointer variable stores the address of a variable. The


declaration int *a doesn't mean that ’a’ is going to contain an integer
value. It means that a is going to contain the address of a variable
storing integer value.

▶ To access the value of a by a pointer


certain address stored variable, * is used. Here, the *
can be read as 'value at‘.

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]

▶ Assuming that the base address of arr is 1000 and each


integer requires two bytes, the five elements will be stored
as follows:

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.

▶ When a function is called by reference any change made to the reference


variable will effect the original variable.

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);

swap(&m, &n); //passing address of m and n to the swap


function printf("After Swapping:\n\n");
printf("m = %d\n", m);

printf("n = %d", n);


return 0;
}
void swap(int *a, int
*b)
{

*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

ptr += 2; *ptr float de-reference of float pointer variable ?


*ptr = 7.0;

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]

ptr += 2; *ptr float de-reference of float pointer variable ?


*ptr = 7.0;

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]

ptr += 2; *ptr float de-reference of float pointer variable 3.14


*ptr = 7.0;

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]

ptr += 2; *ptr float de-reference of float pointer variable ?


*ptr = 7.0;

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]

ptr += 2; *ptr float de-reference of float pointer variable 9.0


*ptr = 7.0;

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]

ptr += 2; *ptr float de-reference of float pointer variable ?


*ptr = 7.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]

ptr += 2; *ptr float de-reference of float pointer variable 6.0


*ptr = 7.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]

ptr += 2; *ptr float de-reference of float pointer variable 3.14


*ptr = 7.0;

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]

ptr += 2; *ptr float de-reference of float pointer variable 7.0


*ptr = 7.0;

41
Summary

▶ A pointer stores the address (memory location) of another entity


▶ Address-of operator (&) gets the address of an entity
▶ De-reference operator (*) makes a reference to the referee of a pointer
▶ Pointer and array
▶ Pointer arithmetic

42