Calloc
Calloc is used to allocates space for an array of elements, initialize values as zero and returns the first address of allocated space to pointer variable.
|
Syntax
(casttype *) calloc(no_of_records as int,size_of_datatype as int);
|
#include
struct product{
int product_no;
char product_name[20];
};
void main(){
typedef struct product pro;
pro *product_ptr;
int i;
//Allocates the memory for structure variable using calloc
product_ptr = (pro *) calloc(5,sizeof(pro));
for (i=0;i<5;i++){
printf("Enter the product no : ");
scanf("%d",&(product_ptr+i)->product_no);
printf("Enter the product name : ");
scanf("%s",&(product_ptr+i)->product_name);
}
for (i=0;i<5;i++){
printf("%d : %s\n",(product_ptr+i)->product_no,(product_ptr+i)->product_name);
}
}
|
Output
Enter the product no : 101 Enter the product name : Hamam Enter the product no : 102 Enter the product name : Liril Enter the product no : 103 Enter the product name : Himalaya Enter the product no : 104 Enter the product name : Colgate Enter the product no : 105 Enter the product name : Lux
101 : Hamam 102 : Liril 103 : Himalaya 104 : Colgate 105 : Lux
|