Lect 9(pointers) Zaheer Abbas

18
Created by Zaheer Abbas Aghani

Transcript of Lect 9(pointers) Zaheer Abbas

Page 1: Lect 9(pointers) Zaheer Abbas

Created by Zaheer Abbas Aghani

Page 2: Lect 9(pointers) Zaheer Abbas

Data structure

lecture 8-9

Pointers

Page 3: Lect 9(pointers) Zaheer Abbas

Pointer

A Pointer provides a way of accessing a variable (or a more complex kind of data, such as an array) without referring to the variable directly. This mechanism used for this is the address of variable.

Page 4: Lect 9(pointers) Zaheer Abbas

What is pointer?

A pointer is a variable which declared as a pointer type. It contains the memory address (locations) of another variable. It doesn’t take the value of the variable.

OR

Pointer is a variable which contains the address in memory of another variable.

Page 5: Lect 9(pointers) Zaheer Abbas

example For example: if one variable is a data type

and second variable is the pointer type which points to the first variable, then the contents of second variable is the address of first variable.

5 2000Value at address

Address of First variable

20003000

First Variable Second Variable

Page 6: Lect 9(pointers) Zaheer Abbas

Uses of pointers

Pointers provides a way of accessing a variable through memory addresses.

Use of pointers, assigns memory space and also releases it. This concept helps us in making the best use of the available memory (dynamic memory allocations).

With pointers, data manipulation is done with address, so the execution time is faster.

Two-dimensional array and multi-dimensional array representation is easy with pointers.

Concept of pointers is used in data structure such as linked list.

Page 7: Lect 9(pointers) Zaheer Abbas

Declaration of pointers

A pointer has to be declared just like any other variable - remember a pointer is just a variable that stores an address.

When a pointer variable is declared then an asterisk (*) symbols should proceed the variable name.

Adding an asterisk in front of a variable's name declares it to be a pointer to the declared type.

Page 8: Lect 9(pointers) Zaheer Abbas

Declaration of pointers

Syntax:-

int *b;

Here b is an integer type of pointer. This shows that the pointer variable b stores address of an integer variable.

The address of any variable is whole number, hence pointer variable also contain whole number.

Page 9: Lect 9(pointers) Zaheer Abbas

Similarly we can declare char and floating type pointer:-

char *p;float *q;

Char *p means ‘p’ contains the address of the variable, which is of char type.

Float *q means, ‘q’ contains the address of the variable which is of floating type.

Page 10: Lect 9(pointers) Zaheer Abbas

Initialization of pointer

When a pointer is declared it does not point anywhere. You must set it to point somewhere before you use it.

Pointer variable can be initialize by following these steps:

1.first declare or initialized an integer variable (suppose int x;)

2.declare a pointer variable3.assign address of variable to pointer variable.

Page 11: Lect 9(pointers) Zaheer Abbas

syntaxint a;int *b;b=&a;

The & operator represents the address of variable.

Here b is a variable which contains the address of variable ‘a’.

Page 12: Lect 9(pointers) Zaheer Abbas

Dereferencing operation of pointer The contents of any variable is referenced by a

pointer variable. For example: ifint x=5;int *b;b=&x;

Now if we want to print the value of x variable by using pointer b. how can it possible?

It is possible by using * operator with pointer name.If we write printf(“%d”,*b) then it print the value of

x.*b performs the "dereferencing" operation on b; it

looks at the address stored in b, and goes to that address and returns the value.

Page 13: Lect 9(pointers) Zaheer Abbas

Programvoid main(void){

int a=5;int *b; //declare pointer variable.b=&a; // assign address of a to pointer variable b.

printf(“value of a =%d”,a);printf(“value of a =%d”,*b);printf(“address of a =%u”,b);printf(“value of b=address of a =%u”,b);

}

Page 14: Lect 9(pointers) Zaheer Abbas

Pointers arithmetic

Arithmetic operation can also be done with pointer variables.

Postfix, prefix, increment, decrement are possible with pointers.

In other variables postfix, prefix, increment, decrement means addition or subtraction by 1 with these variables, but here these operations means addition or subtraction of bytes that pointer data type holds, with the value that pointer variable contain.

Page 15: Lect 9(pointers) Zaheer Abbas

For example: Example:

int a=5;int *b;b=&a;

Here b++ or ++b means 1000+2=1002

& b– or –b means 1000-2=998.

Its doesn’t affect the address of ‘a’.

In other variables

Ex- int a=5;

a++ or ++a means 5+1=6

and a– or –a means 5-1=4.

5 1000

1000 2000

a b

5

20001000

ba

1002

Page 16: Lect 9(pointers) Zaheer Abbas

Pointers and array Array is the collection of similar data types

elements. When we declare an array then consecutive memory locations are allocated to array elements.

For example: int arr[4]={5,10,15,20) Here the base address is the address of 0th

element of array. Since the array is of type int, hence address of next element of array is increment by 2.

Because the array elements are stored in consecutive memory locations therefore when an array pointer is increment it refers to the next location of its data type.

Page 17: Lect 9(pointers) Zaheer Abbas

Example: Let us take an example-

int arr[4]={5,10,15,20}int *a;a=&arr[0]

This shows ‘a’ is a pointer which holds the base address of the array arr or we can write as a=arr;

Now when pointer variable is increment by 1 then it contains the base address+2 b/c pointer is of type int.

Page 18: Lect 9(pointers) Zaheer Abbas

Program:void main(void){

int arr[4]={5,10,15,20};int i=0;int *b;b=arr // we can also write b=&arr[0]for(i=0;i<4;i++){

printf(“value of arr[%d]=%d\n”,i,*b);printf(“address of arr[%d]=%u\n”,i, b);b=b+1;

}