Knowledge Walls
Venkatesan
Hyderabad, Andhra Pradesh, India
Passcode:
Introduction [cont'd]
Input Output Functions [cont'd]
Structures and Unions [cont'd]
# Preprocessor [cont'd]
Calloc in Memory of Programming in C
1976 Views
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);
Program
	#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
Next Topics
Next lessons of current book.
Best Lessons of "Programming in C"
Top lessons which are viewed more times.
Structures and Unions of Programming in C
Input Output Functions of Programming in C
Structures and Unions of Programming in C
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details