introduction to pointers.. what is a pointer? a derived type which holds the address of a variable....

24
Introduction to Pointers.

Upload: evelyn-holland

Post on 31-Dec-2015

246 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Introduction to Pointers.

Page 2: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

What is a pointer?A derived type which holds the address of a variable.Simply a memory location where the variable is stored in system.Pointer is always a whole number, no matter whether it holds the addr of int, char, float etcInt a=100;int *ptr; ptr=&a; Now ptr may be pointing to addr 5000.In the next run of the pgm ‘a’ may be stored in addr 6000 and ptr points to 6000.Pointer will also have an address in memory.

Page 3: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

What is a variable?

• Variable in a program is something with a name, the value of which can vary.

• Ex: int k.When compiler sees this, it reserves space in

memory(4 bytes) to hold an integer value and associate name k with the memory location reserved. This memory reserved is called the address.

Later in the program if it encounters k=2, value 2 will be placed in that memory location.

• Pointer to a variable is nothing but pointer through which we can access this variable in memory.

• Hence through pointers we can change the value of a variable in the memory itself.

• Address of a variable is obtained by prefixing ‘&’ in front of variable.ex: &k

Page 4: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Address in memory• Total memory depends on the size of RAM.• If RAM is 64K(64*1024), then we have

65536 bytes(0 to 65535).• whenever a variable is declared, compiler

allocates memory at appropriate place where the data can be stored.

• For ex: int m=20; Compiler allocates 4 bytes of memory starting from say 2000. Hence the bytes reserved for m are 2000,2001,2002 and 2003.

• Hence a variable is associated with the starting address of allocated memory.

• Programmers do not know the address of the variable.

Page 5: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Why are pointers used? • to perform dynamic memory allocation.• To implement linked lists, trees and

some other data structures.

How is pointer declared?int a=10;int *ptr; //or int * ptr, int* ptr; ptr = &a;

ptr a

print(ptr)2000Print(&a)2000Print(*ptr)10

1020003000 2000

Page 6: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

• Hence ‘*’ before a declaration of a variable says it is a pointer variable.(int *ptr;)

• ‘*’ is not attached to data type during declaration. Hence int* ptr1,ptr2,ptr3. Here, only ptr1 is pointer variable, not ptr2 and ptr3.

• ‘&’ prefixed to a variable gives its address in memory.

• ‘*’ prefixed to a pointer variable gives the value present in the memory location pointed to by pointer.(*ptr)

• ‘&’ is called address operator or indirection operator.

• Address is printed using %u since address is always an unsigned integer.

Page 7: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

• Data type of a pointer variable indicates the type of the variable ,the pointer is pointing to and not the type of pointer.(because pointers are always unsigned integers)Ex:int *ptr1; //ptr1 is pointer to int char *ptr2; //ptr2 is pointer to char

float *ptr3; //ptr3 is pointer to floatHere ptr1,ptr2,ptr3 are all unsigned

integers.• Any number of pointers can point to a single

variable.• A pointer can point to different items at

different statements.

Page 8: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Double pointer or pointer to pointer

int i=3;int *ptr1,**ptr2;ptr1=&i;ptr2=&ptr1;

ptr2 ptr1 i

4800 4500 4000• ** indicates the variable is a pointer to

pointer.• Pointer to pointer holds the address of

another pointer .NOTE: there is no limit to the pointer to

pointers.But maximum limit that is being used is 3.

4500 4000 3

Page 9: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Print(i); //3Print(*ptr1); //3Print(**ptr2); //3Print(ptr2); //4500Print(ptr1); //4000Print(*ptr2); //4000

Ptr2 prints the value contained in its address.i.e 4500

*ptr2 prints the value contained in the address 4500.i.e 4000

**ptr2 prints the value contained in address 4000.i.e 3

Page 10: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

How a variable is stored in memory?

(Assume integer is 2 bytes)int k=5;

This is actually stored as 00 05

int *ptr;ptr=&k;(*ptr)5. when (*ptr) is used to get the

value of k,entire 2 bytes(2000 and 2001) is considered and hence it gives 5.

5

00000000 00000101

2000 2001

k

2000

Page 11: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Arithmetic operations allowed on pointers

1.Incrementing pointer2.Decrementing3.Adding or subtracting a number from the

pointer.4.Subtracting a pointer from a pointer(in case of

array).

Increment• pointers are incremented using ‘++’ operator.• When incremented the pointer points to the

next valid byte.• Increment depends on the type of data, the

pointer is pointing to.Ex: incrementing an integer pointer increments

the pointer by 2 bytes,char pointer by 1 byte and float by 4 bytes.

Page 12: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Decrementing a pointer

• Pointers are decremented using ‘- -’ operator.

• After decrement pointer points to the previous valid byte based on the type of pointer.

Ex:if ptr is an integer pointer and if it points to address 2500.After ptr - -, ptr points to 2498.

if ptr is a floatpointer and if it points to address 2500.After

ptr - -, ptr points to 2496.

Page 13: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Adding or subtracting a number to and from the pointer

ptr=ptr+2; //if ptr==2000. after ptr+2, ptr==2004

ptr=ptr+4 //ptr==2012ptr=ptr-3 //ptr==2006(here ptr is int pointer)

Arithmetic operations not allowed on pointers

1.Addition of 2 pointers.2.Multiplying or dividing a number to a pointer .ptr=ptr*2; //invalidptr=ptr/2;//invalidPtr1+ptr2; //invalid

Page 14: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Ex1:int a, b, c,*p1,*p2,*p3,*p4;a=2;b=3;c=4p1=&a;p2=&b;p3=&c;*p4=a+*p2+*(&c)?Ex2:Int a,*p;P=&a;Printf(“enter the value of a”);Scanf(“%d”,&a);Scanf(“%d”,p);What is wrong in 2nd scanf?

Page 15: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Pointers and functionsArguments are generally passed to functions

by1.Sending the values of arguments.2.Sending the address of arguments.1st method(pass by value)• Value of actual arguments in calling

function is copied onto corresponding formal arguments in called function.

• Changes made are not reflected in actual arguments.

Main(){

int a=10,b=20;print(a); //10print(b); //20

Page 16: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Swap(a,b);Print(a); //10Print(b); //20

}Void swap(int c, int d){

int temp;temp=c;c=d;d=temp;

}Here c==20 and d==10.but actual

arguments not affected.(i.e value not changed in address.

Page 17: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

2nd method(pass by reference)• Passing the address of variables.• Changes are done in the memory. Hence

gets reflected in main as well.Main(){

int a=10,b=20;print(a); //10print(b); //20Swap(&a,&b);Print(a); //20Print(b); //10

}

Page 18: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Void swap( int *c, int *d){

int temp;temp=*c;*c=*d;*d=temp;

}

Function returning pointers• Just like a function can return an int, float,

char etc, it can also return a pointer.• Returning pointer means returning address

of a variable.

Page 19: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Main(){

int *fptr; //int pointerint *func(); //function which returns int pointerfptr=func();print(fptr); //2000print(*fptr); //10

}Int *func(){

static int a=10;return(&a);

}

Page 20: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Pointer compatibility

Main(){

int a=10,*iptr, m;char b,*cptr;iptr=&a;cptr=&b;m=Sizeof(a); //2m=Sizeof(iptr); //4m=Sizeof(b); //1m=Sizeof(cptr); //4

}Pointer of any data type(int,float,char) is 4

bytes long.

Page 21: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

• Even though pointers of different types are of same size (4 bytes),they cannot be assigned to each other.For ex:cptr=iptr or iptr=cptr are illegal.

• This is because these pointers point to different data types.

Void pointer• It is a pointer type to which any pointer can

be assigned and which can be assigned to any pointer.void *pvoid;int *ptr1;char *ptr2;pvoid =ptr1;ptr2=pvoid;

Page 22: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

Lvalue and Rvalue In c,an expression is either an l-value or r-

value.L-value:• This expression is used when the object is

receiving a value.• Usually used in the left side of ‘=‘ operator.

ex:a=…b[5]=…*ptr=…

R-value:Expression that can be used to supply a value.

ex:…=10,….=a+2,….=a*6,…=a[5]+3,a++A variable can act as both l-value or r-value;b=a; a=c;

Page 23: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored

• Operators that need only L-value as operand are:&,++,--,=hence

&(a+2); //error bcoz (a+2) is r-value

&5; //error(a+2)++; //error++(a+3); //error

Pointers should be handled carefully because it actually accesses the memory location and improper use may harm your program and data.

Page 24: Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored