Pointer
Pointer
Pointer
• Pointers are variables that contain memory addresses as their values.
Declaring Pointers
typename *ptrname;
int marks=75;
int *ip;
ip = &marks;
char *ch1, *ch2; /* ch1 and ch2 both are pointers to type char */
float *value, percent; /* value is a pointer to type float, and
/* percent is an ordinary float variable */
Initializing Pointers
pointer = &variable;
p_rate = &rate; /* assign the address of rate to p_rate */
#include <stdio.h>
int var = 1;
int *ptr;
main()
{
/* Initialize ptr to point to var */
ptr = &var;
/* Access var directly and indirectly */
printf("\nDirect access, var = %d", var);
printf("\nIndirect access, var = %d", *ptr);
/* Display the address of var two ways */
printf("\n\nThe address of var = %d", &var);
printf("\nThe address of var = %d\n", ptr);
return 0;
}
2013 Page 1
Ankur Rastogi Unit-II Pointers
Output:
int y = 5;
int *yPtr;
• * (indirection/dereferencing operator)
*yptr = 7; // changes y to 7
Pointer Arithmetic
2013 Page 2
Ankur Rastogi Unit-II Pointers
• When an integer is added to or subtracted from a pointer, the new pointer value is
changed by the integer times the number of bytes in the data variable the pointer is
pointing to.
• For example, if the pointer valptr contains the address of a double precision variable
and that address is 234567870, then the statement:
example-
void main()
int x=7;
int *ptr;
ptr=&x;
ptr=ptr+1;
printf("Value of x=%d\n",x);
printf("Address of x %d\n",ptr);
getch();
• vPtr points to v[2] (incremented by 2), but machine has 4 byte ints.
2013 Page 3
Ankur Rastogi Unit-II Pointers
• Subtracting pointers
vPtr2 = v[2];
vPtr = v[0];
vPtr2 - vPtr == 2.
Example-
void main()
int x[10],i;
int *ptr;
x[0]=7;
ptr=&x[0];
printf("Value of x=%d\n",x[0]);
for(i=0;i<5;i++)
ptr=ptr+1;
*ptr=x[i]+1;
printf("\nAddress of x %d\n",ptr);
2013 Page 4
Ankur Rastogi Unit-II Pointers
printf("Value of x(*ptr)=%d\n",*ptr);
getch();
sizeof(int);
It returns 4 bytes in typical 32 bit machines. Here is a simple program which prints out the
size of almost common C data type.
void main()
2013 Page 5
Ankur Rastogi Unit-II Pointers
getch();
Allocating memory
We use malloc() function to allocate memory. Here is the function interface:
malloc function takes size_t as its argument and returns a void pointer. The void pointer is
used because it can allocate memory for any type. The malloc function will return NULL if
requested memory couldnt be allocated or size argument is equal 0. Here is an example of
using malloc function to allocate memory:
int *ptr;
int size=5;
sizeof(int) return size of integer (4 bytes) and multiply with size which equals 5 so pi pointer
now points to the first byte of 5 * 4 = 20 bytes memory block. We can check whether the
malloc function allocate memory space is successful or not by checking the return value.
if(pi == NULL)
exit(EXIT_FAILURE);
2013 Page 6
Ankur Rastogi Unit-II Pointers
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
a[i]=i+1;
for(i=0;i<n;i++)
printf("%d\n",a[i]);
free(a);
getch();
Beside malloc function, C also provides two other functions which make convenient ways to
allocate memory:
Calloc function not only allocates memory like malloc but also allocates memory for a group
of objects which is specified by num argument.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
a[i]=i+1;
for(i=0;i<n;i++)
printf("%d\n",a[i]);
free(a);
getch();
Realloc function is used to resize the size of memory block, which is already allocated. It
found use of in two situations :
#include<stdio.h>
2013 Page 8
Ankur Rastogi Unit-II Pointers
#include<stdlib.h>
#include<conio.h>
void main()
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
a[i]=i+1;
for(i=0;i<2*n;i++)
printf("%d\n",b[i]);
free(b);
getch();
Freeing memory
When you use malloc to allocate memory you implicitly get the memory from a dynamic
memory pool which is called heap. The heap is limited so you have to deallocate or free the
memory you requested when you don't use it in any more. C provides free function to free
memory. Here is the function prototype:
You should always use malloc and free as a pair in your program to a void memory leak
2013 Page 9
Ankur Rastogi Unit-II Pointers
#include<stdio.h>
#include<conio.h>
int main()
char *msg;
msg=(char *)realloc(msg,50);
free(msg);
getch();
bPtr = b;
OR
bPtr = &b[0]
• Element b[n]
2013 Page 10
Ankur Rastogi Unit-II Pointers
• suit array has a fixed size, but strings can be of any size.
Example 1:-
void main()
char *a[4]={"this","is","string","pointer"},i;
for(i=0;i<4;i++)
puts(a[i]);
getch();
2013 Page 11
Ankur Rastogi Unit-II Pointers
Example 2:-
void main()
int *ptr,a[10],i;
a[0]=1;
ptr=&a[0];
for(i=0;i<10;i++)
ptr=ptr+1;
*ptr=a[0]+i;
printf("Address %d\n",ptr);
printf("value %d\n",*ptr);
getch();
• Pointers can be used to pass addresses of variables to called functions, thus allowing
the called function to alter the values stored there.
• We looked earlier at a swap function that did not change the values stored in the
main program because only the values were passed to the function swap.
• If instead of passing the values of the variables to the called function, we pass their
addresses, so that the called function can change the values stored in the calling
routine. This is known as "call by reference" since we are referencing the variables.
• The following shows the swap function modified from a "call by value" to a "call by
reference". Note that the values are now actually swapped when the control is
returned to main function.
2013 Page 12
Ankur Rastogi Unit-II Pointers
int main ( )
int a = 5, b = 6;
printf("a=%d b=%d\n",a,b) ;
printf("a=%d b=%d\n",a,b) ;
getch() ;
int temp;
2013 Page 13