A37-Assignment 8
A37-Assignment 8
1)Malloc():
#include<stdio.h>
#include<stdlib.h>
int main()
int i,n;
scanf("%d",&n);
int*ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
exit(0);
for(i=0;i<n;i++)
{
printf("Enter an integer:");
scanf("%d",ptr+i);
for(i=0;i<n;i++)
printf("Integer is:%d\n",*(ptr+i));
return 0;
Input:
Enter the number of integers:4
Enter an integer:23
Enter an integer:45
Enter an integer:18
Enter an integer:89
Output:
Integer is:23
Integer is:45
Integer is:18
Integer is:89
2)Calloc():
#include<stdio.h>
#include<stdlib.h>
int main()
int i,n;
scanf("%d",&n);
int*ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
exit(0);
for(i=0;i<n;i++)
printf("Enter an integer:");
scanf("%d",ptr+i);
for(i=0;i<n;i++)
printf("Integer is:%d\n",*(ptr+i));
return 0;
}
Input:
Enter an integer:23
Enter an integer:67
Enter an integer:89
Output:
Integer is:23
Integer is:67
Integer is:89
3)Realloc():
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
char*str;
str=(char*)malloc(3);
strcpy(str,"Puspa");
printf("String=%s,Address=%u\n",str,str);
str=(char*)realloc(str,25);
strcat(str,"raj");
printf("String=%s,Address=%u\n",str,str);
free(str);
printf("String=%s,Address=%u\n",str,str);
return (0);
Output:
String=Puspa,Address=791520
String=Pusparaj,Address=791520
String=,Address=791520
4)Free():
#include<stdio.h>
#include<stdlib.h>
int main()
int i,n;
scanf("%d",&n);
int*ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
for(i=0;i<n;i++)
printf("Enter an integer:");
scanf("%d",ptr+i);
for(i=0;i<n;i++)
printf("Integer is:%d\n",*(ptr+i));
printf("\n");
free(ptr);
for(i=0;i<n;i++)
return 0;
Input:
Enter an integer:56
Enter an integer:78
Enter an integer:23
Enter an integer:98
Enter an integer:27
Output:
Integer is:12
Integer is:56
Integer is:78
Integer is:23
Integer is:98
Integer is:27
union distance
float feet1;
float inch1;
float feet2;
float inch2;
};
int main()
int i;
float sumFeet,sumInch;
for( i=0;i<2;i++){
scanf("%f",&d[i].feet1);
scanf("%f",&d[i].inch1);
scanf("%f",&d[i].feet2);
scanf("%f",&d[i].inch2);
}
sum(d,&sumFeet,&sumInch);
return 0;
int i;
for(i=0;i<2;i++){
*sum_feet=d[i].feet1+d[i].feet2;
*sum_inch=d[i].inch1+d[i].inch2;
Input:
Output:
#include<stdio.h>
int main()
int i;
for(i=Jan;i<=Nov;i++)
printf("%d",month=May);
return 0;
Output:4