2 2+pointers
2 2+pointers
2 Pointers
1 Department of CSE
Objectives
• To understand the need and application of
pointers
• To learn how to declare a pointer and how it is
represented in memory
• To learn the relation between arrays and pointers
• To study the need for call-by-reference
• To distinguish between some special types of
pointers
2 Department of CSE
Agenda
• Basics of Pointers
• Declaration and Memory Representation
• Operators associated with pointers
• address of operator
• dereferencing operator
• Arrays and Pointers.
• Compatibility of pointers
• Functions and Pointers
• Special types of pointers
• void pointer
• null pointer
• constant pointers
• dangling pointers
• pointer to pointer
3 Department of CSE
Introduction
4 Department of CSE
Pointer Declaration
• General form of a pointer variable declaration:-
datatype *ptrname;
• Eg:-
• int *p; (p is a pointer that can point only
integer variables)
• float *fp; (fp can point only floating-point
variables)
6 Department of CSE
Why Pointers?
• Manages memory more efficiently.
9 Department of CSE
Referencing & Dereferencing
Operators
10 Department of CSE
Sample Code -1 : Simple Pointer
#include<stdio.h> Output:-
int main()
{ Number : 10
int x=10;
int *ip;
Address: 0x7fff4fab3044
ip=&x; Number using pointer : 10
printf("Number : %d\n",x);
printf("Address: %p\n",(&x)); Address using Pointer:
printf("Number using pointer : %d\n",0x7fff4fab3044
*ip);
printf("Address using Pointer: %p\
n",ip);
return 0;
}
11 Department of CSE
Sample Code -2 : Pointers to different types
#include<stdio.h> Output:-
int main()
{ Number using pointer : 10
int x=10;
int *ip;
Address using Pointer:
float y=2.5, *fp; 0x7fff4f5c31bc
fp = &y;
ip=&x;
Decimal value : 2.500000
printf("Number using pointer : %d\n",Address of y :
*ip);
0x7fff4f5c31b8
printf("Address using Pointer: %p\
n",ip);
printf("Decimal value : %f\n",*fp);
printf("Address of y : %p\n",fp);
return 0;
}
12 Department of CSE
Sample Code -3 : Same pointer to multiple variables
#include<stdio.h> Output:-
int main()
Enter three integers : 10 20 30
{
int a,b,c,*p; //a,b and c are variables pointer points to a. Value is 10
and p is a pointer pointer points to b. Value is 20
printf("Enter three integers : "); pointer points to c. Value is 30
scanf("%d %d %d",&a,&b,&c);
p = &a;
printf("pointer points to a. Value is
%d\n",*p);
p = &b;
printf("pointer points to b. Value is
%d\n",*p);
p = &c;
printf("pointer points to c. Value is
%d\n",*p);
return 0;
}
13 Department of CSE
Sample Code -4 : Multiple Pointers to same variable
#include<stdio.h> Output:-
int main()
{ Enter an integer : 15
int a;
int *p = &a; 15
int *q = &a; 15
int *r = &a;
15
printf("Enter an integer : ");
scanf("%d",&a);
printf("%d\n",*p);
printf("%d\n",*q);
printf("%d\n",*r);
return 0;
}
14 Department of CSE
Pointer and 1D Array
15 Department of CSE
Relationship between array and pointer
16 Department of CSE
Sample Program 5 : Pointer and Array I
Output:-
#include<stdio.h>
0x7fff03b2d380
int main()
0x7fff03b2d380
{
11
int a[5] = {1,2,3,4,5};
int *p = a;
printf("%p %p\n",&a[0],a);
printf("%d %d\n",*a,*p);
return 0;
}
17 Department of CSE
Sample Program 6 : Pointer and Array II
#include<stdio.h>
Output:-
int main()
First element : 1 1
{
Second element: 2 2
int a[5] = {1,2,3,4,5};
int *p = &a[1];
return 0;
}
Note:-When a pointer to an array is not pointing
to the first element, index can be negative.
18 Department of CSE
Pointer Arithmetic and 1D Arrays
• If ‘a’ is an array name, then ‘a’ points to first
element
• a+1 points to the second element, a+2 points to
third element and so on.
• Generally (a+n) points to (n+1)th element.
21 Department of CSE
Modifying value using pointer
• If ip points to an integer x, *ip can be used in
places where x could have been used.
22 Department of CSE
Sample Code –7: Updating value using Pointer
#include<stdio.h>
int main() Output:-
{ Number using pointer : 10
int x=10;
int *ip; Address using Pointer:
float y=2.5, *fp; 0x7fff76d4f8ec
fp = &y;
Decimal value : 2.500000
ip=&x;
printf("Number using pointer : %d\n", Address of y : 0x7fff76d4f8e8
*ip); Updated Number : 11
printf("Address using Pointer: %p\
n",ip); Updated Number : 55
printf("Decimal value : %f\n",*fp); Updated Number : 56
printf("Address of y : %p\n",fp);
x+=1;
printf("Updated Number : %d\n",
*ip);
*ip *=5;
printf("Updated Number : %d\n",
*ip);
23++*ip;
Department of CSE
Sample Code -8 : Adding two numbers using Pointers
#include<stdio.h>
int main()
Output:-
{ 10 + 5 = 15
int a=10,b=5,c;
int *p1 = &a;
int *p2 = &b;
int *res = &c;
printf("%d + %d = %d\n",*p1,*p2,c);
return 0;
}
24 Department of CSE
Pointer to array : Order of placing ‘*’ and ‘++’
• Assume that z is an inger array with two values 1 and 2 (int z[2]={1,2};)
• Let ip be a pointer to z; (int *ip = z;)
• printf("%d\n", ++*ip);
• increments content in the address pointed by ip.
• z[0]=1 was taken and incremented by 1.
• In output the value is 2
• printf("%d\n", *++ip);
• increments the address pointed by ip.
• ip currently points to z[1]=3
• In output the value is 3
25 Department of CSE
Sample Code Snippet - 9a – ++ before *
#include<stdio.h> • Sample Output
main()
{ 0x7fff0fc66d30
int z[2]={1,3};
int * ip = z; 1
printf("%p\n",ip);
printf("%d\n", *ip);
2
printf("%d\n", ++*ip); 0x7fff0fc66d30
printf("%p",ip);
return 0;
}
26 Department of CSE
Sample Code Snippet -9b : * before ++
#include<stdio.h> • Sample Output
main()
{ 0x7ffffc801740
int z[2]={1,3};
int * ip = z; 1
printf("%p\n",ip);
printf("%d\n", *ip); 3
printf("%d\n", *++ip); 0x7ffffc801744
printf("%p",ip);
return 0;
}
27 Department of CSE
Pointer Compatibility
28 Department of CSE
Pointer Compatibility
• Pointers have a type associated with them
They can point only to specific type.
• Two types:-
• Pointer Size Compatibility
• Pointer Dereferencing compatibility
29 Department of CSE
Pointer Size Compatibility
• Size of all pointers is the same; i.e.; every
pointer variable holds the address of one
memory location. But the size of variable that
the pointer points to can be different.
30 Department of CSE
Sample Code -10 : Pointer Size Compatibility
#include<stdio.h> printf("Size of c : %3d | ",sizeofc);
int main() printf("Size of pc : %3d | ",sizeofpc);
{ printf("size of *pc : %3d\
char c; n",sizeofstarpc);
char* pc; printf("Size of a : %3d | ",sizeofa);
int sizeofc = sizeof(c); printf("Size of pa : %3d | ",sizeofpa);
int sizeofpc = sizeof(pc); printf("size of *pa : %3d\
int sizeofstarpc = n",sizeofstarpa);
sizeof(*pc); printf("Size of d : %3d | ",sizeofd);
printf("Size of pd : %3d | ",sizeofpd);
int a; printf("size of *pd : %3d\
int* pa; n",sizeofstarpd);
int sizeofa = sizeof(a); Sample
return 0; Output
int sizeofpa = sizeof(pa); }
int sizeofstarpa = Size of c : 1 | Size of pc : 8 | size of
sizeof(*pa); *pc : 1
Size of a : 4 | Size of pa : 8 | size of
double d; *pa : 4
double* pd;
int sizeofd = sizeof(d); Size of d : 8 | Size of pd : 8 | size of
31 int sizeofpd = sizeof(pd);
Department of CSE
*pd : 8
int sizeofstarpd =
Dereferencing Compatibility
• Dereference type is the type of variable that the
pointer is referencing.
32 Department of CSE
Sample Code 11: Pointer Dereferencing Incompatibility
#include<stdio.h> ptrcompat.c: In function ‘main’:
int main()
{ ptrcompat.c:13: warning: assignment from
int x=10; incompatible pointer type
int *ip;
float y=2.5, *fp;
fp = &y; Output:-
ip=&x; Number using pointer : 10
printf("Number using pointer :
%d\n", *ip); Address using Pointer:
printf("Address using Pointer: %p\ 0x7fffda19b6ec
n",ip);
Decimal value : 2.500000
printf("Decimal value : %f\n",*fp);
printf("Address of y : %p\n",fp); Address of y : 0x7fffda19b6e8
fp = &x; New value pointed by fp =
printf("New value pointed by fp =
%f\n",*fp); 0.000000
return 0;
}
33 Department of CSE
Pointers and Functions
34 Department of CSE
How to swap two numbers using function?
#include<stdio.h>
int main() Output:-
{ Enter first number : 5
int a,b; Enter second number: 10
void swap(int ,int ); Numbers before function call: 5 10
printf("Enter first number : " ); Numbers before swapping : 5 10
scanf("%d",&a); Numbers after swapping : 10 5
printf("Enter second number: ");
Numbers after function call : 5 10
scanf("%d",&b);
printf("Numbers before function call:
%d\t%d\n",a,b);
swap(a,b);
printf("Numbers after function call : %d\
t%d\n",a,b); void swap(int a, int b)
return 0; {
} int t;
printf("Numbers before swapping : %d\t
%d\n",a,b);
t = a;
a = b;
b = t;
35 Department of CSE printf("Numbers after swapping : %d\t
How to swap two numbers using function?
• Values are getting interchanged inside the
function. But that is not getting reflected in
main.
36 Department of CSE
How to swap two numbers using function?
#include<stdio.h>
int main() Output:-
{ Enter first number : 5
int a,b; Enter second number: 10
void swap(int *,int *); Numbers before function call: 5 10
printf("Enter first number : " ); Numbers before swapping : 5 10
scanf("%d",&a); Numbers after swapping : 10 5
printf("Enter second number: "); Numbers after function call : 10 5
scanf("%d",&b);
printf("Numbers before function call:
%d\t%d\n",a,b);
swap(&a,&b);
printf("Numbers after function call : %d\
t%d\n",a,b); void swap(int *a, int *b)
return 0; {
} int t;
printf("Numbers before swapping : %d\t
%d\n",*a,*b);
t = *a;
*a = *b;
*b = t;
37 Department of CSE printf("Numbers after swapping : %d\t
Points to be noted while using Call-by-Reference
Call-by-Value Call-by-
Reference
Function void swap(int ,int ); void swap(int *,int
Declaration *);
Function void swap(int a, int void swap(int *a,
Header b) int *b)
Function Call swap(a,b); swap(&a,&b);
• Requires ‘*’ operator along with data type of
arguments – in declaration as well as Function
header.
• Requires ‘&’ along with actual arguments in
Function call.
• Requires ‘*’ operator inside function body.
38 Department of CSE
When do you need pointers in
functions?
• First scenario – In Call-by-Reference.
• There is a requirement to modify the values of
actual arguments.
39 Department of CSE
Passing array as an argument to
a function
• When an array is passed as an argument to a
function, it is actually passed as reference.
40 Department of CSE
Sample Code 12 : Passing an array to a function
#include<stdio.h>
int main() void square(int *a,int
{ n)
int a[5]={1,2,3,4,5}; {
int i; int i;
//First argumnt is an array and second for(i=0;i<n;i++)
argument is its size a[i] *= a[i];
void square(int *,int); }
printf("Array before modification:-\n");
for(i=0;i<5;i++)
printf("%d\t",a[i]);
square(a,5);
Output:-
printf("\nArray after modification:-\n");
for(i=0;i<5;i++) Array before modification:-
printf("%d\t",a[i]); 1 2 3 4 5
printf("\n"); Array after modification:-
return 0;
}
1 4 9 16 25
41 Department of CSE
Returning Multiple values from a
function
• Normally, a function can return only a single
value from it, using ‘return’.
42 Department of CSE
Sample Code 13 : Returning Multiple Values
43 Department of CSE
Sample Code 13 : Returning Multiple
Values - Output
Output:-
Number 1 : 2
Number 2 : 1
Number 3 : 3
Number 4 : 5
Number 5 : 4
Minimum element entered : 1
Maximum element entered : 5
44 Department of CSE
Special Pointers
45 Department of CSE
Void Pointer
• A generic type that is not associated with a
reference type.
• It is not the address of a particular data type.
• A pointer with no reference type that can store
only address of any variable.
• Compatible for assignment purposes only with all
other types of pointers.
• A pointer of any reference can be assigned to
void type and vice verse.
• Restriction : It can not be dereferenced unless it
is cast.
• Declaration:- void* pvoid;
•
46 General Casting:- dest_ptr = (dest_ptr_type *)
Department of CSE
NULL Pointer
• A pointer of any type that is assigned the
constant NULL
• The reference type of pointer will not change by
the assignment of NULL
• Eg:-
int* iptr = NULL; //NULL pointer of type ‘int’
Char* cptr = NULL; //NULL pointer of type ‘char’
47 Department of CSE
Dangling Pointer
• Arises during object destruction, when an object that has an incoming
reference is deleted or deallocated, without modifying the value of the
pointer, so that the pointer still points to the memory location of the
deallocated memory.
• Example of creating Dangling pointer
int main()
{
char *ptr=NULL;
{
char c;
ptr = &c;
}
}
• c falls out of scope after the inner block making ptr a dangling pointer
48 Department of CSE
Constant pointer
• A pointer that cannot change the address its holding.
• Once a constant pointer points to a variable then it cannot point to any
other variable.
• Declaration:- <type of pointer> * const <name of pointer>
• Example:- int* const ptr;
• Sample Code : Will give the error 7: error: assignment of read-only variable
‘ptr’
#include<stdio.h>
int main(void)
{
int var1 = 0, var2 = 0;
int *const ptr = &var1;
ptr = &var2;
printf("%d\n", *ptr);
return 0;
}
49 Department of CSE
Pointer to a Pointer
• A pointer points to an address of another pointer.
• Also called Double Pointer.
• Declaration:- type **ptr_name;
• Example:- int **p;
• Sample:-
int main()
{
int p=5;
int *p1 = &p;
int **pp;
pp = &p1;
printf(“Value of P : %d\n”,**pp);
return 0;
}
Output : Value of P : 5
50 Department of CSE
Summary
• Discussed about Pointer and its importance.
• Discussed relationship between array and
pointer
• Discussed about Call-by-Reference and other
places where pointers are needed for functions.
• Discussed special pointers.
51 Department of CSE
References
• Books/Materials
1. C Programming Course – Compiled HTML Help
File
2. Brian W Kernighan, Dennis M Ritchie, “The C
Programming Language”, 2nd Edition, PHI
• Web
1. http://
www.tutorialspoint.com/cprogramming/c_point
ers.htm
2. http://www.cprogramming.com/tutorial/c/
lesson6.html
52 Department of CSE