Load and display the filebigdynl.cfor another example of dynamic allocation. This program is very similar to the last one since we use the same structure, but this time we define an array of pointers to illustrate the means by which you could build a large database using an array of pointers rather than a single pointer to each element. To keep it simple we define 12 elements in the array and another working pointer named "point".
#include<stdio.h>
#include<conio.h>
main( )
{
struct animal {
char name[25];
char breed[25];
int age;
} *pet[12], *point; /*this defines 13 pointers,no variables */
int index;
/* first, fill the dynamic structures with nonsense */
for (index = 0;index < 12;index++) {
pet[index] = (struct animal *)malloc(sizeof(struct animal));
strcpy(pet[index]->name,"General");
strcpy(pet[index]->breed,"Mixed Breed");
pet[index]->age = 4;
}
pet[4]->age = 12; /* these lines are simply to */
pet[5]->age = 15; /* put some nonsense data into */
pet[6]->age = 10; /* a few of the fields. */
/* now prints out the data described above */
for (index = 0;index <12;index++) {
point = pet[index];
printf("%s is a %s, and is %d years old.\n", point->name,
point->breed, point->age);
}
/* good programming practice dictates that we free up the */
/* dynamically allocated space before we quit. */
for (index = 0;index < 12;index++)
free(pet[index]);
}
The "*pet[12]" is new to you so a few words would be in order. What we have defined is an array of 12 pointers, the first being "pet[0]", and the last "pet[11]". Actually, since an array is itself a pointer, the name "pet" by itself is a pointer to a pointer. This is valid in C, and in fact you can go farther if needed but you will get quickly confused. I know of no limit as to how many levels of pointing are possible, so a definition such as "int ****pt" is legal as a pointer to a pointer to a pointer to a pointer to an integer type variable, if I counted right. Such usage is discouraged until you gain considerable experience.
Now that we have 12 pointers which can be used like any other pointer, it is a simple matter to write a loop to allocate a data block dynamically for each and to fill the respective fields with any data desirable. In this case, the fields are filled with simple data for illustrative purposes, but we could be reading in a database, readings from some test equipment, or any other source of data.
A few fields are randomly picked to receive other data to illustrate that simple assignments can be used, and the data is printed out to the monitor. The pointer "point" is used in the printout loop only to serve as an illustration, the data could have been easily printed using the "pet[n]" means of definition. Finally, all 12 blocks of data are freed before terminating the program.
Compile and run this program to aid in understanding this technique. As stated earlier, therewas nothing new here about dynamic allocation, only about an array of pointers.
#include<stdio.h>
#include<conio.h>
main( )
{
struct animal {
char name[25];
char breed[25];
int age;
} *pet[12], *point; /*this defines 13 pointers,no variables */
int index;
/* first, fill the dynamic structures with nonsense */
for (index = 0;index < 12;index++) {
pet[index] = (struct animal *)malloc(sizeof(struct animal));
strcpy(pet[index]->name,"General");
strcpy(pet[index]->breed,"Mixed Breed");
pet[index]->age = 4;
}
pet[4]->age = 12; /* these lines are simply to */
pet[5]->age = 15; /* put some nonsense data into */
pet[6]->age = 10; /* a few of the fields. */
/* now prints out the data described above */
for (index = 0;index <12;index++) {
point = pet[index];
printf("%s is a %s, and is %d years old.\n", point->name,
point->breed, point->age);
}
/* good programming practice dictates that we free up the */
/* dynamically allocated space before we quit. */
for (index = 0;index < 12;index++)
free(pet[index]);
}
The "*pet[12]" is new to you so a few words would be in order. What we have defined is an array of 12 pointers, the first being "pet[0]", and the last "pet[11]". Actually, since an array is itself a pointer, the name "pet" by itself is a pointer to a pointer. This is valid in C, and in fact you can go farther if needed but you will get quickly confused. I know of no limit as to how many levels of pointing are possible, so a definition such as "int ****pt" is legal as a pointer to a pointer to a pointer to a pointer to an integer type variable, if I counted right. Such usage is discouraged until you gain considerable experience.
Now that we have 12 pointers which can be used like any other pointer, it is a simple matter to write a loop to allocate a data block dynamically for each and to fill the respective fields with any data desirable. In this case, the fields are filled with simple data for illustrative purposes, but we could be reading in a database, readings from some test equipment, or any other source of data.
A few fields are randomly picked to receive other data to illustrate that simple assignments can be used, and the data is printed out to the monitor. The pointer "point" is used in the printout loop only to serve as an illustration, the data could have been easily printed using the "pet[n]" means of definition. Finally, all 12 blocks of data are freed before terminating the program.
Compile and run this program to aid in understanding this technique. As stated earlier, therewas nothing new here about dynamic allocation, only about an array of pointers.

