You are on page 1of 26

II

ation

PRESENTED BY
M.YUGANDHAR
CSE DEPARTMENT
AITAM
Definition

datatype *var-name; (OR) datatype* var-name;


Ex: int *ptr; Ex: int* ptr;
Pointers can be categorized into two ways
Key points to remember about pointers in C

•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.

Initialization: int i = 100 ;


int *ptr ;
ptr = &i ;
Following example makes use of these operations

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

•A pointer to a pointer stores the address of another pointer.

•A pointer to a pointer is a form a chain of pointers.

•When we define a pointer to a pointer, the first pointer contains the address of the

second pointer.

•The declaration to declare a pointer to a pointer of type int: int **pptr;


Pointer to Pointer
Pointer to Pointer
#include <stdio.h>
int main ( ) Output:
Value of num = 3000
{ Value at *ptr = 3000
int num; Value at **pptr = 3000
int *ptr;
int **pptr;

num = 3000;
ptr = &num;
pptr =&ptr;
printf("Value of num = %d", num );
printf("Value at *ptr = %d", *ptr );
printf("Value at **pptr = %d", **pptr);
return 0;
}
Memory Allocation

Static memory allocation Dynamic memory allocation


Memory is allocated at compile time. Memory is allocated at run time.
Memory can't be increased while executing Memory can be increased while executing
program. program.
Used in array. Used in linked list.
Dynamic Memory Allocation

•C Dynamic Memory Allocation can be defined as a procedure in which the size of a

memory block is changed during the runtime.

•C provides some functions to achieve these tasks.

•There are 4 library functions provided by C defined under <stdlib.h> header file to

facilitate dynamic memory allocation in C programming.


Dynamic Memory Allocation

•malloc() - allocates single block of memory

•calloc() - allocates multiple block of memory

•free() - frees (delete) the dynamically allocated memory

•realloc() - reallocates the memory

(increase / decrease the size of block)


1.malloc( ) method
•It is used to dynamically allocate a single large block of memory with the specified size.
Syntax: Example:
ptr = (cast-type*) malloc(byte-size) ptr = (int*) malloc (10 * sizeof(int));

•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.

•The malloc() function allocates single block of requested memory.

•On failure, It returns NULL i.e. memory is not sufficient.

•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

type. It initializes each block with a default value ‘0’.


Syntax: Example:
ptr = (cast-type*)calloc(n, element-size); ptr = (float*) calloc(25, sizeof(float));

This statement allocates contiguous space in memory for 25 elements each with the size of

the float.

•The calloc() function allocates multiple block of requested memory.

•It returns NULL if memory is not sufficient


2.calloc( ) method
3.free( ) method
•It is used to dynamically de-allocate the memory.

•Hence the free() method is used, whenever the dynamic memory allocation takes place.

It helps to reduce wastage of memory by freeing it.

Syntax: free(ptr);
3.free( ) method
4.realloc() method
•If the dynamically allocated memory is insufficient or more than required.

• It is used to dynamically change the memory allocation of a previously allocated

memory.

•In other words, if the memory previously allocated with the help of malloc or calloc is

insufficient, realloc can be used to dynamically re-allocate memory.

•where ptr is reallocated with new size 'newSize'.

•Syntax: ptr = realloc(ptr, newSize);


4.realloc() method

You might also like