Professional Documents
Culture Documents
Agenda
5 Pointer to Functions
6 Types of Pointers
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
INTRODUCTION
Introduction
Syntax –
Example –
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction
int *pointer;
/* pointer points to an integer */
double *ptr2;
/* pointer to a double */
float *ptr3;
/* pointer to a float */
char *ch1 ;
/* pointer to a character */
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction
Two operators used:-
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
HOW TO USE POINTERS?
How to use Pointers?
#include<stdio.h>
i
void main(){
int i = 200; 200
int* pointer; 7058
pointer = &i;
printf(“%d\n”,i);
printf(“%u\n”,pointer);
printf(“%d\n”,pointer); pointer
printf(“%d\n”,&i);
printf(“%u\n”,&i); 7058
printf(“%d\n”,&pointer);
printf(“%u\n”,&pointer);
printf(“%d\n”,*pointer); 4586
printf(“%d\n”,*(&i));
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
SIZE OF POINTERS
Size of Pointers
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Size of Pointers
#include<stdio.h>
Size of char* is: 1
struct student{
Size of int* is: 2
int regno;
Size of student* is: 26
char sname[20];
float avg;
};
Size of char* is: 4
int main(){
Size of int* is: 4
char *cptr;
Size of student* is: 4
int *iptr;
struct student* sptr;
printf("Size of char* is: %d\n",sizeof(cptr));
printf("Size of int* is: %d\n",sizeof(iptr));
printf("Size of student* is: %d\n",sizeof(sptr));
return 0;
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Size of Pointers
char c = ‘gl’ short s = 14 student* sptr = *stu;
Size short*
char*
of char*sptr
cptr
is:== *s;
4*c;
Size of int* is: 4
gl 14 6422177
6422172
6422190
Size of short* is: 4
Size of double* is: 4
6422172 [1 byte] 6422177 [2 bytes] [4
[4 bytes]
bytes]is: 4
Size of student*
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
PROGRAM ON
SWAPPING OF TWO
VALUES
Using call by value
#include <stdio.h>
void swap(int , int);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values a = %d, b = %d\n",a,b);
}
void swap (int x, int y)
{
int temp;
temp = x;
x=y;
y=temp;
printf("After swapping values in function x = %d, y = %d\n",x,y);
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Using call by reference
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values a = %d, b = %d\n",a,b);
}
void swap (int *x, int *y)
{
int temp;
temp = *x;
*x=*y;
*y=temp;
printf("After swapping values in function x = %d, y = %d\n",*x,*y);
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
POINTER TO
FUNCTION
Pointer to Function
Syntax:
Declaration:
This pointer can point to any function which is taking two int arguments and returns
int.
Function:
#include <stdio.h>
int main()
{
int *p = NULL;
printf(“The value inside variable p is:\n%x”,p);
return 0;
}
Void pointer:
#include <stdio.h>
int main()
{
void *p = NULL;
printf("The size of pointer is:%d\n",sizeof(p));
return 0;
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Types of Pointers
• Wild pointer
• Dangling pointer
• Complex pointer
• Near pointer
• Far pointer
• Huge pointer
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
POINTER TO
POINTER
Pointer to Pointer
#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a;
pp = &p;
printf("address of a: %x\n",p);
printf("address of p: %x\n",pp);
printf("value stored at p: %d\n",*p);
printf("value stored at pp: %d\n",**pp);
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
POINTER WITH
ARRAY AND STRING
Pointer with Array
#include <stdio.h>
int main()
{
int a[5]={1,2,3,4,5}; //array initialization
int *p;
p=a;
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pointer with Strings
#include <stdio.h>
#include <string.h>
int main()
{
char str[]=“Great Learning!";
char *p;
p=str;
printf("First character is:%c\n",*p);
p =p+1;
printf("Next character is:%c\n",*p);
printf("Printing all the characters in a string\n");
p=str;
for(int i=0;i<strlen(str);i++)
{
printf("%c\n",*p);
p++;
}
return 0;
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
ADVANTAGES OF
POINTERS
Advantages of Pointers
• Pointers provide an efficient way for accessing the elements of an array structure.
• Pointers are used to form complex data structures such as linked list, graph, tree, etc.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Thank You
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited