Pointers
Pointers
POINTERS
Session Objectives
. What is meant by pointer is and why it can ne
used?
. How to Declare and access a pointer
variable
. Explain Pointer
Increment/Decrement
. Explain the use of pointers with
arrays
. Explain How Pointer To Functions can be
used
. Explain Pointers to Structures can be
used
POINTERS
For Example :
#include<stdio.h>
void main()
{
int val=100;
printf("%u\n",&val);
printf("%d\n",val);
printf("%d\n",*(&val));
}
Why are Pointers Used ?
To return more than one value from a function
To pass arrays & strings more conveniently from one function to
another
To manipulate arrays more easily by moving pointers to them, Instead
of moving the arrays themselves
To allocate memory and access it (Dynamic Memory Allocation)
To create complex data structures such as Linked List, Where one
data structure must contain references to other data structures
Advantages:
A pointer enables us to access a variable that is defined outside the function.
Pointers are more efficient in handling the data tables.
Pointers reduce the length and complexity of a program.
They increase the execution speed.
The use of a pointer array to character strings results in saving of data
storage space in memory.
The function pointer can be used to call a function
Pointer arrays give a convenient method for storing strings
Many of the ‘C’ Built-in functions that work with strings use Pointers
It provides a way of accessing a variable without referring to the variable
directly
#include<stdio.h>
#include<conio.h>
void main()
{
int n=10;
int *ptr;
ptr=&n;
printf("Value of n is %d",n);
printf("\nAddress of n is %x",&n);
printf("\nAddres of pointer is %x",ptr);
printf("\nvalue stored in pointer is %d",*ptr);
getch();
}
Example 2
#include<stdio.h>
#include<stdlib.h>
#define size 10
void main()
{
char name[size];
char *i;
printf("\n Enter your name ");
gets(name);
i=name;
printf("\n Now printing your name is :");
while(*i != '\0')
{
printf("%c",*i);
i++;
}
}
Explain how the variable can be accessed by pointer
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float a,*b;
clrscr();
printf("\n Enter the radius of the circle");
scanf("%d",&r);
a=3.14*r*r;
b=&a;
printf("\n The value of a=%f",a);
printf("\n The value of a=%u",&a);
printf("\n The value of b=%u",b);
printf("\n The value of a=%f",*b);
getch();
}
Pointer Arithmetic
. Addition and subtraction are the only operations that can
be performed on pointers.
. Then ptr_var has the value 1000 stored in it. Since integers
are 2 bytes long, after the expression “ptr_var++;” ptr_var
will have the value as 1002 and not 1001.
Pointer Increment process example
#include<stdio.h>
#include<conio.h>
void main()
{
int *ptr; //static memory allocation
clrscr();
ptr=(int *) malloc(sizeof(int));
*ptr=100;
printf("\n%u\n",ptr); //address of ptr
printf("\n%d\n",*ptr);
ptr++; // increment 2 bytes
*ptr=101;
printf("\n%d\n",*ptr);
free(ptr);
getch();
}
/* Note : int *ptr=100 means 100 is a address
*ptr=100 or 101 means 100 is a value */
HINTS