* Dynamic Memory Allocation * idea: useful to request memory as the program runs. * example: ask the user for the number of elements in an array int main() { int *values; int n; printf("Enter number of elements: "); scanf("%d", &n); //Use "calloc" to request memory: values = (int *) calloc (n, sizeof(int)); //Now we can treat "values" like an ordinary array! values[0] = 1; .... //Use "free" to "release" memory: free(values); } Notes on calloc: * returns a pointer to an array of "chunks" of memory * 2nd arg = size of each chunk * 1st arg = # of elements (chunks) in the array * calloc returns a "generic pointer", so you need to "cast it" to a pointer of the appropriate type. Notes on free: * "releases" the array that the pointer points to * Example 2: an array of structures #include #include typedef struct { char title[50]; char artist[50]; int rating; } CD; int main() { CD *cdlist; int n,i; printf("Enter number of elements: "); scanf("%d", &n); cdlist = (CD *) calloc (n, sizeof(CD)); for (i = 0; i < n; i++) { strcpy(cdlist[i].title,"unknown"); strcpy(cdlist[i].artist,"unknown"); cdlist[i].rating = 0; } .... free (cdlist); return 0; } * Example 3: copying a string to another string 1 #include 2 #include 3 int main() 4 { 5 char name1[] = "Sarah"; 6 char *name2; 7 int n; 8 n = strlen(name1); 9 name2 = ( char * ) calloc ( n+1, sizeof(char) ); 10 strcpy(name2, name1); 11 printf("%s\n", name1); 12 printf("%s\n", name2); 13 return 0; 14 } * Example 4: An Array of Pointers 1 #include 2 #include 3 #include 4 int main (void) 5 { 6 char s[50]; 7 char *names[100]; //names is an array of pointers to char 8 int i,n,l; 9 printf("enter number of names: "); 10 scanf("%d", &n); 11 for (i = 0; i < n; i++) { 12 printf("enter name: "); 13 scanf("%s", s); 14 l = strlen(s); 15 names[i] = (char *) calloc (l+1, sizeof(char)); 16 strcpy(names[i], s); 17 } 18 for (i = 0; i < n; i++) 19 printf("%s\n", names[i]); 20 for (i = 0; i < n; i++) 21 free(names[i]); 22 return 0; 23 } * Example 5: Sorting An Array of Pointers 1 #include 2 #include 3 #include 4 void sort(char *list[], int n) 5 int main (void) 6 { 7 char s[50]; 8 char *names[100]; 9 int i,n,l; 10 printf("enter number of names: "); 11 scanf("%d", &n); 12 for (i = 0; i < n; i++) { 13 printf("enter name: "); 14 scanf("%s", s); 15 l = strlen(s); 16 names[i] = (char *) calloc (l+1, sizeof(char)); 17 strcpy(names[i], s); 18 } 19 sort(names,n); 20 for (i = 0; i < n; i++) 21 printf("%s\n", names[i]); 22 for (i = 0; i < n; i++) 23 free(names[i]); 24 return 0; 25 } 26 void sort(char *list[], int n) 27 { 28 int pass, i; 29 char *temp; 30 for (pass = 0; pass < n-1; pass++) 31 for (i = 0; i < n-1; i++ ) 32 if ( strcmp(list[i],list[i+1]) > 0 ) { 33 temp = list[i]; 34 list[i] = list[i+1]; 35 list[i+1] = temp; 36 } 37 } * Example 6: Sorting A 2-D Array 1 #include 2 #include 3 #include 4 void sort(char list[][50], int n) 5 int main (void) 6 { 7 char s[50]; 8 char names[100][50]; 9 int i,n,l; 10 printf("enter number of names: "); 11 scanf("%d", &n); 12 for (i = 0; i < n; i++) { 13 printf("enter name: "); 14 scanf("%s", s); 15 strcpy(names[i],s); 16 } 17 sort(names,n); 18 for (i = 0; i < n; i++) 19 printf("%s\n", names[i]); 20 return 0; 21 } 22 void sort(char list[][50], int n) 23 { 24 int pass, i; 25 char temp[50]; 26 for (pass = 0; pass < n-1; pass++) 27 for (i = 0; i < n-1; i++ ) 28 if ( strcmp(list[i],list[i+1]) > 0 ) { 29 strcpy(temp,list[i]); 30 strcpy(list[i],list[i+1]); 31 strcpy(list[i+1], temp); 32 } 33 }