Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers A pointer is a variable which stores the address of another variable A pointer is a derived data type in C which is constructed from fundamental.

Similar presentations


Presentation on theme: "Pointers A pointer is a variable which stores the address of another variable A pointer is a derived data type in C which is constructed from fundamental."— Presentation transcript:

1 Pointers A pointer is a variable which stores the address of another variable A pointer is a derived data type in C which is constructed from fundamental data type of C language Pointer can be used to access and manipulate data stored in the memory

2 . Advantages By using pointers we can access data which is available outside the function When we are implementing dynamic memory allocation then we are using pointers Pointers can increase the execution speed of the program Any type of data structure required to implement using pointers only

3 . Syntax:- datatype*pointerVariable;
According to the syntax ‘*’ indirection operator must be required between datatype and pointer name. Space is not mandatory between datatype and pointer name eg:- int*ptr; char*ptr; Datatype*pointervariable;

4 . When we are working with pointers required to use following operators &(address of) *(indirection or deference or object at location or value at address) ‘&’ always return base address of a variable, starting cell address of a variable is called as base address. ‘*’(indirection operator) always returns value of address

5 . Example:- #include<stdio.h> void main() { int a; int*ptr; ptr=&a; a=10; printf(“\n%p%p”,&a,ptr); printf(“\n%d%d”,a,*ptr); *ptr=20; printf(“\n%u%u”,&a,ptr); }

6 . Output:- 0XFFFF4 0XFFFF In implementation when we are printing an address of a variable then recommended to use %p or %x or %u or %lu or %lp format specifiers only. %p or %x or %u >prints 16 bit address %lu or %lp >prints 32 bit physical address %p or %x or %lp >prints address in hexadecimal form %u or %lu >prints address in decimal format

7 . a *ptr ptr Gr 10 20 Gr 65524

8 . Example 2:- void main() { int a,b; int*ptr; ptr=&a; a=10; b=20; printf(“%p%p%p”,&a,&b,ptr); printf(“\n%d%d%d”,a,b,*ptr); *ptr=30; ptr=&b; b=40; printf(“\n%x%x%x”,&a,&b,ptr); printf(“%d%d%d”,a,b,*ptr); }

9 . Pointers for inter function Communication:-
One of the most useful applications of pointers is functions Passing address Functions returning pointers Passing address:- To pass pointers to a function , we have to specify the address in the function call. To receive those address we have to declare pointer variable in the function definition.

10 . /* Program to illustrate about call by Reference */ #include<stdio.h> #include<conio.h> //void Swap(int *,int *); void Swap(int*pa,int*pb) { int c; printf("\nin swap x=%u \t y=%u\n",&pa,&pb); c=*pa; *pa=*pb; *pb=c; printf("\nThe values after swap in swap function *pa=%d *pb=%d",*pa,*pb); } void main() int a=10,b=20; printf("\n In main ,The values before swap are a=%d b=%d\n",a,b); Swap(&a,&b); printf("\n In main, The values after swap are a=%d b=%d\n",a,b);

11 . Output:- In main ,The values before swap :a=10 b=20 Swap x=65518 y=65520 The values after swap in swap function *pa=20 *pb=10 In main, The values after swap are a=20 b=10

12 Functions returning the pointers:- In the below example we are doing functions as Pointers which is returning the address of a variable. #include<stdio.h> int *big(int*,int*); void main() { int a=10,b=20; int*p; p=big(&a,&b); printf("BIG is %d",*p); } int *big(int*pa,int*pb) if(*pa>*pb) return(pa); else return(pb); .

13 . Pointer to Pointer:- It is a procedure of holding the address of a pointer into another pointer variable. In C programming language pointer to pointer relations cam be applied up to 12-stages only Syntax:- pointer : datatype*ptr; pointer to pointer : datatype**pptr; Pointer to pointer to pointer: datatype***ppptr;

14 . Example:- void main() { int i; int *ptr; int**pptr; int***ppptr; ptr=&i; pptr=&ptr; ppptr=&pptr; i=10; printf(“%d %d %d %d”,***ppptr,**pptr,*ptr,i); } Output:

15 . Compatibility:- Compatibility means assigning the address of variable of some data type to a pointer of same type. We cannot assign one type of address to another type of pointer. Compatibility should be considered in the following three cases 1. Pointer Size 2. Deference type 3. Deference level

16 . Pointer Size Compatibility
The pointer size is same for memory addresses which is of 2 bytes(size of a memory address of a computer) The size of all variables to which a pointer is referring will be different. Pointer size depends on compiler not on data type 16 bit bytes 32 bit bytes 64 bit bytes

17 . Example:- void main() { int a=5,*pa=&a; float b=6,*pb=&b; char=‘c’,*pc=&c; printf(“\n size of a=%d int=%d pa=%d”,sizeof(a),sizeof(int),sizeof(pa)); printf(“\n size of b=%d float=%d pb=%d”,sizeof(b),sizeof(float),sizeof(pb)); printf(“\n size of c=%d char=%d pc=%d”,sizeof(c),sizeof(char),sizeof(pc)); }

18 . De-Reference type Compatibility:-
The de-reference type is data type of a variable to which a pointer is referencing. Except in one case, we cannot assign the address of one data type to another data type Eg:- int a; char *p; p=&a;--->invalid because of different types The incompatibility can be solved by typecasting char*p; p=(char*)&a;

19 . Example:- void main() { int a=65; char*p; p=(char*)&a; printf(“\na=%d a=%c *p=%d \ *p=%c”,a,a,*p,*p); printf(“\n*p+1=%d *p+1=%c”,*p+1,*p+1); } NOTE:- This type compatibility won’t works for typecasting the other conversions like float--->int and float--->char

20 . Void pointer:- Generic pointer of C and C++ is called void pointer
Generic pointer means it can access and manipulate any kind of data properly The size of the void pointer is 2 bytes or 4 bytes or 8 bytes depends on compiler When we are working with void pointer always type specification will be decided at the time of execution By using void pointer when we are accessing the data then type casting must be required

21 . Without type casting if we are accessing the data then it gives an error. On void pointers it is not possible to perform arithmetic operations.

22 . Example:- void main() { int i; float f; char ch; int*iptr=(int*)0; float*fptr=(float*)0; char*cptr=(char*)0; iptr=&i; i=10; printf(“\n%d%d”,i,*iptr); fptr=&f; printf(“\n%d%d”,f,*fptr); cptr=&ch; ch=‘A’; printf(“\n%c%c”,ch,*cptr); }

23 . Output:- 10 12.3 12.3 A A Explanation:-
A A Explanation:- In previous program in place of creating multiple pointers recommended to create a single pointer variable which can handle all 3 types of variables properly i.e void pointer required to use.

24 . Example:- void main() { int i; float f; char ch; void*ptr; ptr=&i; i=10; printf(“%d%d”,i,*(int*)ptr); ptr=&f; f=12.3; printf(“\n%f%f”,f,*(float*)ptr); ptr=&ch; ch=‘A’; printf(“\n%c%c”,c,*(char*)ptr); }

25 . Deference level Compatibility
Compatibility should be compared in pointer level also. A pointer to int is not compatible with a pointer-to-pointer to int has a reference type of pointer to int. eg:- int a; int*pa; int**ppa; pa=&a; ppa=&pa;

26 Pointers and Arrays:- The array name is by default a pointer containing address of first element. Eg:- int a[5]={10,20,30,40,50}; We can print the first element in a array using index or a pointer. eg:- printf(“%d %d”,a[0],*a); Output: a[0] and *a represents the value in the first memory location i.e 10 To access the next element we have to increase the variable “a”

27 Eg:- a+1 represents the address of second element
a+2 represents the address of third element a+n can be calculated using the formula a+n*(size of the element) We can increment or decrement a pointer variable to access the values in an array For example if a pointer variable p contains an address of a second element then p– will move the pointer to first element We can access these values using the indirection operator ”*”.

28 . //pointer to arrays as pointer void main() { int a[3]={6,7,8}; clrscr(); for(i=0;i<3;i++) printf(“\na[%d]=%d a[%d]=%d a[%d]=%d “,i,a[i],i,*(a+i),i,*a+i); getch(); } Output:- a[0]=6 a[0]=6 a[0]=6 a[1]=7 a[1]=7 a[1]=7 a[2]=8 a[2]=8 a[2]=8

29 . //Program to find the sum of n elements of an // array using pointers void main() { int a[10],n,i,sum=0; int*p; p=&a[0]; printf(“\n How many no’s you need to sum”); scanf(“%d”,&n); for(i=0;i<n;i++) printf(“\n Enter elemet %d”,i+1); scanf(“%d”,&a[i]); sum+=*(p+i); } printf(“\n\n sum=%d”,sum); getch();

30 . Pointers to Functions:-
Like variables a function has an address in memory We can store the address in a pointer variable and using that variable we call the function Useful in call back functions, function call at runtime, sorting and search functions Syntax:- datatype(*pntr)(); The data type is the return type of the function After declaring a variable we have to assign the name to the pointer variable

31 . //program to illustrate about the pointer function void show() { printf(“\n show”); } void main() void (*p)(); clrscr(); p=&show; p(); //show(); getch(); Output:- show

32 . // pointer to function with argument int add(int x,int y) { return x+y; } void main() int(*p)(int,int); int a=5,b=6,c; p=&add; c=p(a,b); printf(“\nsum=%d”,c); Output:- sum=11


Download ppt "Pointers A pointer is a variable which stores the address of another variable A pointer is a derived data type in C which is constructed from fundamental."

Similar presentations


Ads by Google