introduction to java - wordpress.com · introduction •pointer is a variable that stores/points to...

59
1

Upload: others

Post on 25-Sep-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

1

Page 2: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Introduction• Pointer is a variable that stores/points to the address of another variable.

• It is a derived data type in C.

• It is a built from the fundamental data types.

• A variable name directly references a value.

• A pointer variable must be declared before it can be used.

• Each memory cell in the computer has an address that can be used to access thatlocation so a pointer variable points to a memory location we can access andchange the contents of this memory location via the pointer.

2

Page 3: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Introduction• Pointer is a variable that stores/points to the address of another variable.

• C pointer is used to allocate memory dynamically i.e. at runtime.

• i is the name given for particular memory location of ordinary variable.

• Let us consider, its corresponding address be 65524 and the value stored in variable ‘i’is 3.

• The address of the variable ‘i’ is stored in another integer variable whose name is ‘j’and which is having corresponding address 65522.

• Reference pointer (&) : If var is a variable then,&var is the address in memory.

• j = &i i.e. j = Address of i

3

Page 4: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Introduction• Declaration of pointer

• Dereference pointer(*) are used for defining pointer variable.

• data_type* pointer_variable_name;

• int* p; p as pointer variable of type int.

• & symbol is used to get the address of the variable.

• * symbol is used to get the value of the variable that the pointer is pointing to. This isknown as indirection operator or dereferencing operator. It is a unary operator.

• Common mistakes:

• int c, *pc;

• pc=c; /* pc is address whereas, c is not an address. */

• *pc=&c; /* &c is address whereas, *pc is not an address. */

4

Page 5: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

5

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

Page 6: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

6

Computer Memory Revisited

• Altering the value of a variable is indeed changing the content of the memory• e.g. i = 40; a[2] = ‘z’;

Addr Content Addr Content Addr Content Addr Content

1000 i: 40 1001 j: 46 1002 k: 58 1003 m: 74

1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘z’ 1007 a[3]: ‘\0’

1008 ptr: 1001 1009 … 1010 1011

Page 7: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

7

Addressing Concept

• Pointer stores the address of another entity

• It refers to a memory location

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 */

Page 8: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

8

Why do we need Pointer?

• Simply because it’s there!

• It is used in some circumstances in C

Remember this?

scanf(“%d”, &i);

Page 9: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

9

What actually ptr is?

• ptr is a variable storing an address

• ptr is NOT storing the actual value of i

int i = 5;

int *ptr;

ptr = &i;

printf(“i = %d\n”, i);

printf(“*ptr = %d\n”, *ptr);

printf(“ptr = %p\n”, ptr);

5i

address of iptr

Output:

i = 5

*ptr = 5

ptr = effff5e0

value of ptr =

address of i

in memory

Page 10: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

10

Twin Operators

• &: Address-of operator• Get the address of an entity

• e.g. ptr = &j;

Addr Content Addr Content Addr Content Addr Content

1000 i: 40 1001 j: 33 1002 k: 58 1003 m: 74

1004 ptr: 1001 1005 1006 1007

Page 11: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

11

Twin Operators

• *: De-reference operator• Refer to the content of the referee

• e.g. *ptr = 99;

Addr Content Addr Content Addr Content Addr Content

1000 i: 40 1001 j: 99 1002 k: 58 1003 m: 74

1004 ptr: 1001 1005 1006 1007

Page 12: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

12

Example: Pass by Reference

• Modify behaviour in argument passing

void f(int j)

{

j = 5;

}

void g()

{

int i = 3;

f(i);

}

void f(int *ptr)

{

*ptr = 5;

}

void g()

{

int i = 3;

f(&i);

} i = ?i = ?i = 3 i = 5

Page 13: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

13

An Illustration

int i = 5, j = 10;

int *ptr;

int **pptr;

ptr = &i;

pptr = &ptr;

*ptr = 3;

**pptr = 7;

ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable 5

j int integer variable 10

Page 14: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

14

An Illustration

int i = 5, j = 10;

int *ptr; /* declare a pointer-to-integer variable */

int **pptr;

ptr = &i;

pptr = &ptr;

*ptr = 3;

**pptr = 7;

ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable 5

j int integer variable 10

ptr int * integer pointer variable

Page 15: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

15

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;

**pptr = 7;

ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable 5

j int integer variable 10

ptr int * integer pointer variable

pptr int ** integer pointer pointer variable

Double

Indirection

Page 16: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

16

An Illustration

int i = 5, j = 10;

int *ptr;

int **pptr;

ptr = &i; /* store address-of i to ptr */

pptr = &ptr;

*ptr = 3;

**pptr = 7;

ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable 5

j int integer variable 10

ptr int * integer pointer variable address of i

pptr int ** integer pointer pointer variable

*ptr int de-reference of ptr 5

Page 17: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

17

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;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable 5

j int integer variable 10

ptr int * integer pointer variable address of i

pptr int ** integer pointer pointer variable address of ptr

*pptr int * de-reference of pptr value of ptr

(address of i)

Page 18: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

18

An Illustration

int i = 5, j = 10;

int *ptr;

int **pptr;

ptr = &i;

pptr = &ptr;

*ptr = 3;

**pptr = 7;

ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable 3

j int integer variable 10

ptr int * integer pointer variable address of i

pptr int ** integer pointer pointer variable address of ptr

*ptr int de-reference of ptr 3

Page 19: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

19

An Illustration

int i = 5, j = 10;

int *ptr;

int **pptr;

ptr = &i;

pptr = &ptr;

*ptr = 3;

**pptr = 7;

ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable 7

j int integer variable 10

ptr int * integer pointer variable address of i

pptr int ** integer pointer pointer variable address of ptr

**pptr int de-reference of de-reference of pptr

7

Page 20: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

20

An Illustration

int i = 5, j = 10;

int *ptr;

int **pptr;

ptr = &i;

pptr = &ptr;

*ptr = 3;

**pptr = 7;

ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable 7

j int integer variable 10

ptr int * integer pointer variable address of j

pptr int ** integer pointer pointer variable address of ptr

*ptr int de-reference of ptr 10

Page 21: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

21

An Illustration

int i = 5, j = 10;

int *ptr;

int **pptr;

ptr = &i;

pptr = &ptr;

*ptr = 3;

**pptr = 7;

ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable 7

j int integer variable 9

ptr int * integer pointer variable address of j

pptr int ** integer pointer pointer variable address of ptr

**pptr int de-reference of de-reference of pptr

9

Page 22: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

22

An Illustration

int i = 5, j = 10;

int *ptr;

int **pptr;

ptr = &i;

pptr = &ptr;

*ptr = 3;

**pptr = 7;

ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable 7

j int integer variable 9

ptr int * integer pointer variable address of i

pptr int ** integer pointer pointer variable address of ptr

*pptr int * de-reference of pptr value of ptr

(address of i)

Page 23: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

23

An Illustration

int i = 5, j = 10;

int *ptr;

int **pptr;

ptr = &i;

pptr = &ptr;

*ptr = 3;

**pptr = 7;

ptr = &j;

**pptr = 9;

*pptr = &i;

*ptr = -2;

Data Table

Name Type Description Value

i int integer variable -2

j int integer variable 9

ptr int * integer pointer variable address of i

pptr int ** integer pointer pointer variable address of ptr

*ptr int de-reference of ptr -2

Page 24: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointer Declaration• Pointer Declaration Style:

• int* p; /*Style 1*/

• int *p; /*Style 2*/

• int * p; /*Style 3*/

• Style 2 is popular because, convenient to have multiple declaration in the samestatement. Like,

• int *p, x, *q;

• This style matches with the format used for accessing the target values.• For example,

• int x, *p, y;

• x=10;

• p = &x;

• y= *p;

• *p= 20;24

Page 25: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Initialization of pointer variables• The Process of assigning the address of a variable to a pointer variable is known asinitialization.

• Way1: int quantity;int *p;

p=&quantity;

• Way 2: int quantity;

int *p=&quantity;

• Way 3: int x, *p=&x; (valid)

int *p=&x, x; (invalid)

• Way 4: int *p = NULL;

• Way 5: int *p = o;

int *p = 5655; (invalid)25

Page 26: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Initialization of pointer variables• Pointer flexibility:

• We can make the same pointer to point to different data variables indifferent statements.

• For example,

int x, y, z, *p;

………….

p=&x;

………….

p=&y;

………….

p=&z;

………….

26

Page 27: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Initialization of pointer variables• Pointer flexibility:

• We can also use different pointers to point to same data variable.

• For example,

int x;

int *p1 = &x;

int *p2 = &x;

int *p3 = &x;

• The two statements,• p=&q;

• n=*p;

• are equivalent to• n=*&q;

• which in turn is equivalent to• n=q; 27

Page 28: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointer Arithmetic

28

Page 29: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

29

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!!!

Page 30: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

30

Pointer Arithmetic and Array

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Data Table

Name Type Description Value

a[0] float float array element (variable) ?

a[1] float float array element (variable) ?

a[2] float float array element (variable) ?

a[3] float float array element (variable) ?

ptr float * float pointer variable

*ptr float de-reference of float pointer variable

?

Page 31: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

31

Pointer Arithmetic and Array

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Data Table

Name Type Description Value

a[0] float float array element (variable) ?

a[1] float float array element (variable) ?

a[2] float float array element (variable) ?

a[3] float float array element (variable) ?

ptr float * float pointer variable address of a[2]

*ptr float de-reference of float pointer variable

?

Page 32: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

32

Pointer Arithmetic and Array

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Data Table

Name Type Description Value

a[0] float float array element (variable) ?

a[1] float float array element (variable) ?

a[2] float float array element (variable) 3.14

a[3] float float array element (variable) ?

ptr float * float pointer variable address of a[2]

*ptr float de-reference of float pointer variable

3.14

Page 33: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

33

Pointer Arithmetic and Array

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Data Table

Name Type Description Value

a[0] float float array element (variable) ?

a[1] float float array element (variable) ?

a[2] float float array element (variable) 3.14

a[3] float float array element (variable) ?

ptr float * float pointer variable address of a[3]

*ptr float de-reference of float pointer variable

?

Page 34: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

34

Pointer Arithmetic and Array

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Data Table

Name Type Description Value

a[0] float float array element (variable) ?

a[1] float float array element (variable) ?

a[2] float float array element (variable) 3.14

a[3] float float array element (variable) 9.0

ptr float * float pointer variable address of a[3]

*ptr float de-reference of float pointer variable

9.0

Page 35: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

35

Pointer Arithmetic and Array

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Data Table

Name Type Description Value

a[0] float float array element (variable) ?

a[1] float float array element (variable) ?

a[2] float float array element (variable) 3.14

a[3] float float array element (variable) 9.0

ptr float * float pointer variable address of a[0]

*ptr float de-reference of float pointer variable

?

Page 36: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

36

Pointer Arithmetic and Array

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Data Table

Name Type Description Value

a[0] float float array element (variable) 6.0

a[1] float float array element (variable) ?

a[2] float float array element (variable) 3.14

a[3] float float array element (variable) 9.0

ptr float * float pointer variable address of a[0]

*ptr float de-reference of float pointer variable

6.0

Page 37: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

37

Pointer Arithmetic and Array

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Data Table

Name Type Description Value

a[0] float float array element (variable) 6.0

a[1] float float array element (variable) ?

a[2] float float array element (variable) 3.14

a[3] float float array element (variable) 9.0

ptr float * float pointer variable address of a[2]

*ptr float de-reference of float pointer variable

3.14

Page 38: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

38

Pointer Arithmetic and Array

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Data Table

Name Type Description Value

a[0] float float array element (variable) 6.0

a[1] float float array element (variable) ?

a[2] float float array element (variable) 7.0

a[3] float float array element (variable) 9.0

ptr float * float pointer variable address of a[2]

*ptr float de-reference of float pointer variable

7.0

Page 39: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

39

Pointer Arithmetic and Array

• Type of a is float *

•a[2] *(a + 2)

ptr = &(a[2])

ptr = &(*(a + 2))

ptr = a + 2

•a is a memory address constant

•ptr is a pointer variable

float a[4];

float *ptr;

ptr = &(a[2]);

*ptr = 3.14;

ptr++;

*ptr = 9.0;

ptr = ptr - 3;

*ptr = 6.0;

ptr += 2;

*ptr = 7.0;

Page 40: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointer Increment

40

Page 41: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointer Increment

41

Page 42: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

void pointer• Suppose we have to declare integer pointer, character pointer and float pointer, then we

need to declare 3 pointer variables.

• Instead of declaring different types of pointer variable it is feasible to declare singlepointer variable which can act as integer pointer, character pointer.

• In C, General Purpose Pointer is called as void Pointer.

• It does not have any data type associated with it.

• It can store address of any type of variable.

• A void pointer is a C convention for a raw address.

• The compiler has no idea what type of object a void Pointer really points to?

• Declaration of void pointer : void *pointer_name;

• Why use void pointer ? Reusability of pointers

42

Page 43: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

void pointer• void *ptr; // ptr is declared as Void pointer

char cnum;int inum;float fnum;ptr = &cnum; // ptr has address of character dataptr = &inum; // ptr has address of integer dataptr = &fnum; // ptr has address of float data

• We have declared 3 variables of integer, character and float type.

• When we assign address of integer to the void pointer, pointer will become Integer

Pointer.

• When we assign address of Character Data type to void pointer it will become

Character Pointer.

• Similarly we can assign address of any data type to the void pointer.

• It is capable of storing address of any data type

43

Page 44: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointers and Arrays• int arr[4];

• Name of the array is actually a pointer that points to the first element of thearray.

• Here, address of first element of an array is &arr[0].

• Also, arr represents the address of the pointer where it is pointing. Hence,&arr[0] is equivalent to arr.

• Also, value inside the address &arr[0] and address arr are equal.

• Value in address &arr[0] is arr[0] and value in address arr is *arr. Hence, arr[0] isequivalent to *arr.

44

Page 45: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointers and Arrays• Similarly,

• &a[1] is equivalent to (a+1) AND, a[1] is equivalent to *(a+1).

• &a[2] is equivalent to (a+2) AND, a[2] is equivalent to *(a+2).

• &a[3] is equivalent to (a+1) AND, a[3] is equivalent to *(a+3).

• .

• &a[i] is equivalent to (a+i) AND, a[i] is equivalent to *(a+i).

45

Page 46: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointers and Arrays

46

Page 47: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointers and Arrays

47

Page 48: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointers and Arrays

48

Page 49: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointers and Arrays

• Program for accessing array elements and

printing array elements in reverse using

pointer.

49

Page 50: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Some programs on arrays using pointer

• Program for accessing array elements and print array elements in reverse orderusing pointer.

• Program for sorting n numbers in an array using pointer.

• Program for finding reverse of a string using pointer.

• Program to manipulate string using pointer (i.e find length, compare, concatenateand copy string)

50

Page 51: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointer to pointer (Double pointer)• A pointer to a pointer is a form of multiple

indirection, or a chain of pointers.

• Normally, a pointer contains the address of avariable.

• When we define a pointer to a pointer, the firstpointer contains the address of the secondpointer, which points to the location thatcontains the actual value as shown below.

• int **var;

51

Page 52: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Pointers and functions – Call by reference• When, argument is passed using pointer, address of the memory location is

passed instead of value.

• The address of memory location num1 and num2 are passed to function and thepointers *a and *b accept those values. So, the pointer a and b points to addressof num1 and num2 respectively. When, the value of pointer are changed, thevalue in memory location also changed correspondingly. Hence, change made to*a and *b was reflected in num1 and num2 in main function.

52

Page 53: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Function Pointers• A function pointer is used for address of function and can be declared as :

• <return type of function> (*<name of pointer>) (type of function arguments)

• For example :• int (*fptr)(int, int)

• The above line declares a function pointer ‘fptr’ that can point to a functionwhose return type is ‘int’ and takes two integers as arguments

• Name of the function can be treated as starting address of the function so wecan assign the address of function to function pointer using function’s name.

• Unlike normal pointers, a function pointer points to code, not data. Typically afunction pointer stores the start of executable code.

53

Page 54: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Function Pointers

54

Page 55: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Function returning a pointer• Till now, we have seen functions that take arguments as pointers and returning

only the standard data type.

• We can have a function which does not return a value but a pointer to a value.

• The syntax is data_type *func_name(arguments);

• Here, * before the name of a function means that the functions returns a pointerof the data_type mentioned. So, in the calling function the value must beassigned to a pointer of the appropriate type.

55

Page 56: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Function returning a pointer• In this case, must be careful because local variables of function does not live

outside the function, hence if you return a pointer connected to a local variable,that pointer will be pointing to nothing when function ends.

56

Page 57: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Advantages of Pointers1. Pointers are more efficient in handling arrays and data tables.

2. Pointers reduce length and complexity of programs.

3. They increase the execution speed and thus reduce the program execution time.

4. Pointers can be used to return multiple values from a function via functionarguments.

5. The use of pointer arrays to character strings results in saving of data storagespace in memory.

6. Pointer permit references to functions and thereby facilitating passing offunctions as arguments to other functions.

7. Pointers allow C to support dynamic memory management.

8. Pointers provide an efficient tool for manipulating dynamic structures such asstructures, linked lists, stacks.

57

Page 58: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

Disadvantages of Pointers1. Uninitialized pointers might cause segmentation fault.

2. Dynamically allocated block needs to be freed explicitly. Otherwise, it wouldlead to memory leak.

3. Pointers are slower than normal variables.

4. If pointers are updated with incorrect values, it might lead to memory corruption

58

Page 59: Introduction to JAVA - WordPress.com · Introduction •Pointer is a variable that stores/points to the address of another variable. •C pointer is used to allocate memory dynamically

ANY QUESTIONS??

59