3. Introduction
• A Pointer is a derived data type in C.
• Pointers contains memory addresses as their
values.
• Pointer reduces the length and complexity of
the program.
• It allows working with dynamically allocated
memory.
4. • Definition:
Pointer variable: Pointer variable is a variable
that contains an address, which is a location of
another variable in memory.
6. Initialization of pointer variable
• The process of assigning the address of a
variable to a pointer variable is known as
initialization.
• Initialization:
int *p=#
7. Example
• W.a.p to illustrate the use of indirection operator ‘*’ to access the value pointed to
by a pointer.
#include<stdio.h>
void main()
{
int x,y;
int *ptr;
x=10;
ptr=&x;
y=*ptr;
clrscr();
printf(“n%d”,x);
printf(“n %d is stored at address %u”,x,&x);
printf(“n %d is stored at address %u”,*&x,&x);
printf(“n %d is stored at address %u”,*ptr,ptr);
8. printf(“n %u is stored at address %u”,ptr,&ptr);
printf(“n %d is stored at address %u”,y,&y);
*ptr=30;
printf(“n now x=%d”,x);
getch();
}
Output:
Value of x =10
10 is stored at address 65524
10 is stored at address 65524
10 is stored at address 65524
65524 is stored at address 65520
10 is stored at address 65522
Now x =30
9. Pointer to pointer
• A variable that is pointer to pointer must be
declared using additional indirection operator
symbols in front of the name.
int **p2;
10. Example
w.a.p to demonstrate the pointer to pointer.
#include<stdio.h>
void main()
{
char c=‘z’,*cp,**pcp;
float f=10.2;*fp,**pfp;
int i=987,*ip,**pip;
cp=&c;
pcp=&cp;
fp=&f;
pfp=&fp;
ip=&i;
pip=&ip;
clrscr();
printf(“n”);
printf(“n c=%c, cp=%u, pcp=%u”, c,&cp,&pcp);
12. Pointers and Arrays
w.a.p to find out maximum number from 1-D array using pointer.
#include<stdio.h>
void main()
{
int a[5],i;
int *p,max=0;
clrscr();
p=a;
for(i=0;i<5;i++)
{
printf(“ a[%d]”,i);
scanf(“%d”,p);
p++;
}
14. Arrays of pointers
• Arrays of pointers is a collection of pointers of
same data type.
• Syntax:
data type *name[size];
15. Example
Demonstrate the use of pointer in 2-D.
#include<stdio.h>
void main()
{
int m[2][2];
int i,j;
clrscr();
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“nEnter value for row = %d,column= %d:-”,i,j);
17. Output:
Enter the value for row=0, column=0:-1
Enter the value for row=0, column=1:-2
Enter the value for row=1, column=0:-3
Enter the value for row=1, column=1:-4
m[0][0]=1 m[0][1]=2
m[1][0]=3 m[1][1]=4