You are on page 1of 29

POINTERS IN C

Agenda

1 Introduction 7 Pointer to Pointer

2 How to use Pointers? 8 Pointer with Array and Strings

3 Size of Pointers 8 Advantages of Pointers

4 Swapping of two values

5 Pointer to Functions

6 Types of Pointers

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
INTRODUCTION
Introduction

● Pointer points to a memory location.

● It is used to access the information/value from the memory.

● It is a variable that stores the address of another variable.

Syntax –

Datatype* Identifier; / Datatype *Identifier;

Example –

int* a; / int *a;

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction
int *pointer;
/* pointer points to an integer */

int *ptr1, pointer;


/* ptr1 is a pointer to type integer and pointer is an integer variable */

double *ptr2;
/* pointer to a double */

float *ptr3;
/* pointer to a float */

char *ch1 ;
/* pointer to a character */

float *ptr, variable;


/*ptr is a pointer to type float and variable is an ordinary float variable */

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction
Two operators used:-

&  Address operator *  Pointer operator

Address operator returns


Pointer variable returns
the address(memory
the value which is inside
location) of a particular
specified address.
variable.

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

Datatype Size Compiler int size Pointer


char 1 byte type size
32 bit 2 bytes 4 bytes
short 2 bytes
64 bit 4 bytes 8 bytes
int 2 or 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes

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*

double d = 65.78 student struct - stu

65.78 regno sname avg

6422187 [8 bytes] 6422190 [26 bytes]

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:

return_type (*Identity) (args_list);

Declaration:

int (*ptr) (int,int);

This pointer can point to any function which is taking two int arguments and returns
int.

Function:

int add(int x, int y)


{
int z = x + y;
return z;
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pointer to Function
#include <stdio.h>
int add(int,int);
int main()
{
int a,b,result;
int (*ip)(int,int);
printf("Enter the values of a and b : ");
scanf("%d %d",&a,&b);
ip=add;
result=(*ip)(a,b);
printf("Value after addition is : %d",result);
return 0;
}
int add(int a,int b)
{
int c=a+b;
return c;
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
TYPES OF
POINTERS
Types of Pointers
NULL pointer:

#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

Other 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;

printf("Printing the array elements using pointer\n");


for(int i=0;i<5;i++)
{
printf("\n%x",*p);
p++;
}
return 0;
}

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 are useful for accessing memory locations.

• Pointers provide an efficient way for accessing the elements of an array structure.

• Pointers are used for dynamic memory allocation as well as deallocation.

• 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

You might also like