C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Dynamic Memory Allocation - calloc function

C Programming

In this tutorial we will learn about calloc function to dynamically allocate memory in C programming language.

The calloc function

We use the calloc function to allocate memory at run time for derived data types like arrays and structures.

Using calloc function we can allocate multiple blocks of memory each of the same size and all the bytes will be set to 0. This is different from the malloc function from the previous tutorial which is used to allocate single block of memory space.

Calloc syntax

Following is the syntax of the calloc function to allocate memory dynamically.

ptr = (cast_type *) calloc (n, element_size);

Where, ptr is a pointer of type cast_type.

n is the total block of contiguous spaces, each of size element_size to be allocated using the calloc function.

Calloc example

In the following example we are allocating memory space for 3 student structure variable.

// student structure
struct student {
  char id[10];
  char firstname[64];
  char lastname[64];
  int score;
};

// new type
typedef struct student candidate;

// student structure pointer
candidate *sptr;

// variables
int no_of_students = 3;

// allocate memory blocks
sptr = (candidate *) calloc (no_of_students, sizeof(candidate));

In the above code we are allocating 3 blocks of memory space each of size candidate structure i.e., 140 bytes.

MemberData TypeSize
idchar10 bytes
firstnamechar64 bytes
lastnamechar64 bytes
scoreint2 bytes

Write a program in C to dynamically allocate memory using calloc function for a structure

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    
  // student structure
  struct student {
    char id[10];
    char firstname[64];
    char lastname[64];
    int score;
  };
  
  // new type
  typedef struct student candidate;
  
  // student structure pointer
  candidate *sptr;
  candidate *tmp;
  
  // variables
  int no_of_students = 3;
  int i;
  
  // allocate memory blocks
  sptr = (candidate *) calloc (no_of_students, sizeof(candidate));
  
  // get student details
  for(i = 0, tmp = sptr; i < no_of_students; i++, tmp++) {
    printf("Enter detail of student #%d\n", (i+1));
    printf("ID: ");
    scanf("%s", tmp->id);
    printf("First Name: ");
    scanf("%s", tmp->firstname);
    printf("Last Name: ");
    scanf("%s", tmp->lastname);
    printf("Score: ");
    scanf("%d", &tmp->score);
  }
  
  // display student details
  printf("\n\nFollowing are the student details:\n\n");
  for(i = 0, tmp = sptr; i < no_of_students; i++, tmp++) {
    printf("Detail of student #%d\n", (i+1));
    printf("ID: %s\n", tmp->id);
    printf("First Name: %s\n", tmp->firstname);
    printf("Last Name: %s\n", tmp->lastname);
    printf("Score: %d\n", tmp->score);
  }
  
  // free memory location
  free(sptr);

  return 0;
}

Output:

Enter detail of student #1
ID: s01
First Name: Yusuf
Last Name: Shakeel
Score: 8
Enter detail of student #2
ID: s02 
First Name: Jane
Last Name: Doe
Score: 9
Enter detail of student #3
ID: s03
First Name: John
Last Name: Doe
Score: 7


Following are the student details:

Detail of student #1
ID: s01
First Name: Yusuf
Last Name: Shakeel
Score: 8
Detail of student #2
ID: s02
First Name: Jane
Last Name: Doe
Score: 9
Detail of student #3
ID: s03
First Name: John
Last Name: Doe
Score: 7

We can represent the allocated memory as follows.

So, the three structures each of size 140 bytes are allocated memory location from address 1000 to 1139, 2000 to 2139 and 3000 to 3139.

The sptr structure pointer points at these memory locations.

We are using a temporary tmp structure pointer to access these allocated memory blocks.