Professional Documents
Culture Documents
ation
PRESENTED BY
M.YUGANDHAR
CSE DEPARTMENT
AITAM
Definition
•Normal variable stores the value whereas pointer variable stores the address of the
variable.
•& symbol is used to get the address of the variable.
•* symbol is used to get the value of the variable that the pointer is pointing to.
•The size of any pointer is 2 byte .
Initialization of Pointers?
•There are few important operations, which we will do with the help of pointers very
frequently.
(a) we define a pointer variable
(b) assign the address of a variable to a pointer and
(c) finally access the value at the address available in the pointer variable.
int main( )
{ Note: In the example, p is a pointer, not
int *p, a; *p. You cannot and should not do
a = 5; something like *p = &a;
p = &a;
printf("%d", *p);
return 0;
}
Output: 5
Pointer arithmetic
•C pointer is an address, which is a numeric value. Therefore, you can perform arithmetic
operations on a pointer just as you can a numeric value.
•There are four arithmetic operators that can be used on pointers: ++, --, +, -
•We prefer using a pointer in our program instead of an array because the variable pointer
can be incremented,array name which cannot be incremented.
•The following program increments the variable pointer to access each succeeding
element of the array:
Incrementing a Pointer
#include <stdio.h>
int main ( )
{
int a[ ] = {10, 100, 200};
int i, *ptr;
ptr = a;
for ( i = 0; i < 3; i++)
{
printf(" %d ", *ptr );
ptr ++;
}
return 0;
}
Output:
10
100
200
#include <stdio.h>
int main()
{
int m = 5, n = 10, a,b,c,d;
int *p1, *p2, *p3;
p1 = &m;
p2 = &n;
a =( *p1)+(*p2);
b =( *p1)-(*p2);
c =( *p1)*(*p2);
d =( *p1)/(*p2);
printf("*p1+*p2 = %d", a);
printf("*p1 -*p2 = %d", b);
printf("*p1 * *p2 = %d", c);
printf("*p1 /*p2 = %d", d);
}
Pointer to Pointer
•When we define a pointer to a pointer, the first pointer contains the address of the
second pointer.
num = 3000;
ptr = #
pptr =&ptr;
printf("Value of num = %d", num );
printf("Value at *ptr = %d", *ptr );
printf("Value at **pptr = %d", **pptr);
return 0;
}
Memory Allocation
•There are 4 library functions provided by C defined under <stdlib.h> header file to
•Since the size of int is 4 bytes, this statement will allocate 40 bytes of memory.
• The pointer ptr holds the address of the first byte in the allocated memory.
•On success, It returns base address of memory block i.e. memory is sufficient
1.malloc( ) method
Example: for (i = 0; i < n; i++)
#include <stdio.h> {
#include <stdlib.h> ptr[ i ] = i + 10;
}
int main()
{
int *ptr;
int n = 5, i; printf("The elements of the array are: ");
ptr = (int*)malloc(n * sizeof(int) ); for (i = 0; i < n; i++)
if (ptr == NULL) {
{ printf("%d ", ptr[i]);
printf("Memory not allocated."); }
exit(0); }
return 0;
}
else
}
{ Output:
printf("Memory successfully allocated using malloc."); successfully allocated using malloc.
The elements of the array are: 10 11 12 13 14
2.calloc( ) method
•It is used to dynamically allocate the specified number of blocks of memory of the specified
This statement allocates contiguous space in memory for 25 elements each with the size of
the float.
•Hence the free() method is used, whenever the dynamic memory allocation takes place.
Syntax: free(ptr);
3.free( ) method
4.realloc() method
•If the dynamically allocated memory is insufficient or more than required.
memory.
•In other words, if the memory previously allocated with the help of malloc or calloc is