Pointers
Created By:
Name: Abhimanyu Mehta
V.V.P Engineering College, Rajkot
Pointers
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.
• Definition:
Pointer variable: Pointer variable is a variable
that contains an address, which is a location of
another variable in memory.
Declaring pointer variables
• The declaration of the pointer variable takes
the following form:
data_type *pt_name;
Initialization of pointer variable
• The process of assigning the address of a
variable to a pointer variable is known as
initialization.
• Initialization:
int *p=#
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);
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
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;
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);
printf(“n i=%d, ip=%u, pip=%u”, i,&ip,&pip);
printf(“n f=%f, fp=%u, pfp=%u”, f,&fp,&pfp);
getch();
}
Output:
c=z,cp=65522,pcp=65520
i=987,ip=65508,pip=65506
f=10.200000,fp=65514,pfp=65512
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++;
}
{
max=*p;
}
p++;
}
printf(“n max=%d”,max);
getch();
}
Output:
Enter the value for a[0]= 12
Enter the value for a[1]= 23
Enter the value for a[2]= 3
Enter the value for a[3]= 40
Enter the value for a[4]= 51
Max=51
Arrays of pointers
• Arrays of pointers is a collection of pointers of
same data type.
• Syntax:
data type *name[size];
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);
scanf(“%d”,m[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(“m[%d][%d]=%d”,i,j,*(*(m+i)+j));
}
printf(“n”);
}
getch();
}
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
THANK YOU

Pointers

  • 1.
    Pointers Created By: Name: AbhimanyuMehta V.V.P Engineering College, Rajkot
  • 2.
  • 3.
    Introduction • A Pointeris 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.
  • 5.
    Declaring pointer variables •The declaration of the pointer variable takes the following form: data_type *pt_name;
  • 6.
    Initialization of pointervariable • The process of assigning the address of a variable to a pointer variable is known as initialization. • Initialization: int *p=&num;
  • 7.
    Example • W.a.p toillustrate 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 isstored 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 demonstratethe 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);
  • 11.
    printf(“n i=%d, ip=%u,pip=%u”, i,&ip,&pip); printf(“n f=%f, fp=%u, pfp=%u”, f,&fp,&pfp); getch(); } Output: c=z,cp=65522,pcp=65520 i=987,ip=65508,pip=65506 f=10.200000,fp=65514,pfp=65512
  • 12.
    Pointers and Arrays w.a.pto 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++; }
  • 13.
    { max=*p; } p++; } printf(“n max=%d”,max); getch(); } Output: Enter thevalue for a[0]= 12 Enter the value for a[1]= 23 Enter the value for a[2]= 3 Enter the value for a[3]= 40 Enter the value for a[4]= 51 Max=51
  • 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 useof 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);
  • 16.
  • 17.
    Output: Enter the valuefor 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
  • 18.