Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program that illustrates the use of malloc and calloc functions

Posted By: Vilhelm Fischer     Category: C Programming     Views: 3999

Write a program that illustrates the use of malloc and calloc functions.

Code for Program that illustrates the use of malloc and calloc functions in C Programming

#include <stdio.h>
#include <malloc.h>
main()
{
int  *base;     \\ A
int i;        
int cnt=0;
int sum=0;
printf("how many integers you have to store \n");    
scanf("%d",&cnt);        \\ B
base = (int *)malloc(cnt * sizeof(int));    \\ C
printf("the base of allocation is %16lu \n",base);    \\ D
if(!base)    \\ E
  printf("unable to allocate size \n");
else
 {        
    for(int j=0;j<cnt;j++)        \\ F
    *(base+j)=5;
 }
 sum = 0;
 for(int j=0;j<cnt;j++)            \\ G
    sum = sum + *(base+j);
 printf("total sum is %d\n",sum);
 free(base);        \\ H
 printf("the base of allocation is %16lu \n",base);
 base = (int *)malloc(cnt * sizeof(int));
printf("the base of allocation is %16lu \n",base);
 base = (int *)malloc(cnt * sizeof(int));        \\ I
printf("the base of allocation is %16lu \n",base);
 base = (int *)calloc(10,2);                \\ H
printf("the base of allocation is %16lu \n",base);
}
  
Share: 



Vilhelm Fischer
Vilhelm Fischer author of Program that illustrates the use of malloc and calloc functions is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!