Wednesday 28 September 2011


ALLOCATE MEMORY FOR ARRAY USING CALLOC

Aim:
To write a program in C to allocate memory for array using calloc.

Algorithm:
1. Start the program.
2. Declare the pointer variable.
3. Allocate the memory space using mallocQ.
4. If pointerData = = NULL, allocation is not done. Else done successfully.
5. Get number of items to be stored and the numbers.
6. Print the entered numbers
7. Stop the program.

Program:
#include <stdio.h>
#include <stdlib.h>
int main 0
{
int i, n;
int *pojnterData;
printf (“Enter number of items to be stored: “);
scanf (“%d”, &i);
pointerData = (int*) calloc (i, sizeof(int));
if (pointerData==NULL)
exit (1);
for (n =0; n < i; n++)
{
printf (“Enter number #%d: “, n);
scanf (“%d”, &pointerData[ n
}
printf (“You have entered: “);
for (n =0; n <i; n++)
printf (“%d “, pointerData[ n
free (pointerData);
return 0;
}

Output
Enter number of items to be stored: 4
Enter number #0:3
Enter number #1:2
Enter number #2:1
Enter number #3:5
You have entered: 3 2 1 5
Result:
Thus the program is written and executed to allocate memory for array using calloc.

0 comments:

Post a Comment