Dynamic Memory Allocation
Spring Semester 2011 Programming and Data Structure 51
Dynamic Memory Allocation Spring Semester 2011 Programming and Data - - PowerPoint PPT Presentation
Dynamic Memory Allocation Spring Semester 2011 Programming and Data Structure 51 Basic Idea Many a time we face situations where data are dynamic in nature. Amount of data cannot be predicted beforehand. Number of data items keeps
Spring Semester 2011 Programming and Data Structure 51
Spring Semester 2011 Programming and Data Structure 52
Spring Semester 2011 Programming and Data Structure 53
Spring Semester 2011 Programming and Data Structure 54
Spring Semester 2011 Programming and Data Structure 55
Spring Semester 2011 Programming and Data Structure 56
Spring Semester 2011 Programming and Data Structure 57
Spring Semester 2011 Programming and Data Structure 58
Spring Semester 2011 Programming and Data Structure 59
Spring Semester 2011 Programming and Data Structure 60
Spring Semester 2011 Programming and Data Structure 61
printf("Input heights for %d students \n",N); for (i=0; i<N; i++) scanf ("%f", &height[i]); for(i=0;i<N;i++) sum += height[i]; #include <stdio.h> main() { int i,N; float *height; float sum=0,avg;
Spring Semester 2011 Programming and Data Structure 62
avg = sum / (float) N; printf("Average height = %f \n", avg); free (height); } printf("Input no. of students\n"); scanf("%d", &N); height = (float *) malloc(N * sizeof(float));
#include <stdio.h> typedef struct { int roll; char dept_code[25]; float cgpa; } stud; for (k=0; k<n; k++) scanf (”%d %s %f”, &class[k].roll, class[k].dept_code, &class[k].cgpa); for (j=0; j<n-1; j++) for (k=1; k<n-j; k++) { if (class[k-1].roll >
Spring Semester 2011 Programming and Data Structure 63
main() { stud *class, t; int j, k, n; scanf (”%d”, &n); /* no. of students */ height = (stud *) malloc(n * sizeof(stud)); class[k].roll) { t = class[k-1]; class[k-1] = class[k]; class[k] = t; } } <<<< PRINT THE RECORDS >>>> }
Spring Semester 2007 Programming and Data Structure 64
Spring Semester 2011 Programming and Data Structure 65
Spring Semester 2011 Programming and Data Structure 66