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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/
#include<stdio.h>
#include<stdlib.h>
int main() {
int* grades;
int sum = 0, i, numberOfStudents;
float average;
printf("Enter the number of students: ");
scanf("%d", &numberOfStudents);
getchar();
if((grades = (int*) malloc(numberOfStudents * sizeof(int))) == NULL) {
printf("nError: Not enough memory to allocate grades arrayn");
exit(1);
}
printf("nEnter the grades of %d students (in separate lines):n", numberOfStudents);
for(i = 0; i < numberOfStudents; i++) {
scanf("%d", &grades[i]);
getchar();
}
/* 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",
average);
getchar();
return(0);
}
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 Follow @c_program
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!