Feb 15, 2012

C Program to demonstrate dynamic memory allocation example.

Write a C Program to demonstrate dynamic memory allocation example.
Dynamic memory allocation means you can allocate or relocate (manipulate) the memory at the run time, using malloc, calloc, and realloc functions.
Using malloc, We can allocate block of memory for a variable
Using calloc function, We can allocate multiple blocks of memory for a variable.
We can alter, reassign the allocated memory using the realloc function.
We can releasing the allocated memory using the free function. Read more about C Programming Language .

* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*                      Happy Coding


int main() {
 int* grades;
 int sum = 0, i, numberOfStudents;
 float average;

 printf("Enter the number of students: ");
 scanf("%d", &numberOfStudents);

 if((grades = (int*) malloc(numberOfStudents * sizeof(int))) == NULL) {
  printf("\nError: Not enough memory to allocate grades array\n");

 printf("\nEnter the grades of %d students (in separate lines):\n", numberOfStudents);

 for(i = 0; i < numberOfStudents; i++) {
  scanf("%d", &grades[i]);

 /* calculate sum */
 for(i = 0; i < numberOfStudents; i++)
  sum += grades[i];

 /* calculate the average */
 average = (float) sum / numberOfStudents;

 printf("\nThe average of the grades of all students is %.2f", 


Read more Similar C Programs
Learn C Programming

Number System
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link