ALLOCATE MEMORY FOR ARRAY USING CALLOC - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Wednesday, October 27, 2010

ALLOCATE MEMORY FOR ARRAY USING CALLOC

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
#include
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 printf (“%d “, pointerData[ n
free (pointerData);
return 0;
}

No comments:

Post a Comment