Objective[edit | edit source]
|
Lesson[edit | edit source]Introduction[edit | edit source]Structures allow objects of different types to be stored within one object. Let's say that you want to store a car's colour, maximum speed and registration number. We can define a structure that includes all that information. First, include the header files required for #include<stdio.h> #include<string.h> Define the structure: structcar { unsignedintcolour; unsignedintmax_speed; charreg_num[10]; }; Example of the structure being used: intmain(void) { /* define two car objects */ structcarcar_1,car_2; car_1.colour=1;/* initialize car_1's colour */ car_1.max_speed=200;/* initialize car_1's speed */ /* initialize the reg number */ strcpy(car_1.reg_num,"MG 100"); /* copy car_1 data to car_2 */ car_2=car_1; /* print the values */ printf("Colour = %u\n",car_2.colour); printf("Speed = %u\n",car_2.max_speed); printf("Reg = %s\n",car_2.reg_num); return0; } Dynamic Structures[edit | edit source]Along with being static, structures can also be dynamic. This means that they can be created and deleted at run time. An example is given below. #include<stdio.h> // This is a basic dog structure. structdog { char*name; char*breed; char*coat; intage; }; // Describes the dog. voiddescribe(structdog*d) { printf("This is a %s dog. The %s's name is %s. %s has a nice %s coat. %s is %d years old.\n\n", d->breed,d->breed,d->name,d->name,d->coat,d->name,d->age); } intmain(void) { // Allocate memory for all of the dogs. // Notice that size for malloc is struct dog and it's casted as struct dog*. structdog*dog1=(structdog*)malloc(sizeof(structdog)); structdog*dog2=(structdog*)malloc(sizeof(structdog)); structdog*dog3=(structdog*)malloc(sizeof(structdog)); // Give the dogs some personality. dog1->name="Foxtrot"; dog1->breed="American Eskimo"; dog1->coat="white"; dog1->age=6; dog2->name="Delta"; dog2->breed="German Shepherd"; dog2->coat="tan with a black saddle"; dog2->age=3; dog3->name="Whiskey"; dog3->breed="Tamaskan"; dog3->coat="wolf grey"; dog3->age=2; // Tell the user about each dog. describe(dog1); describe(dog2); describe(dog3); // Free the allocated memory. free(dog1); free(dog2); free(dog3); return0; } OUTPUT This is a American Eskimo dog. The American Eskimo's name is Foxtrot. Foxtrot has a nice white coat. Foxtrot is 6 years old. This is a German Shepherd dog. The German Shepherd's name is Delta. Delta has a nice tan with a black saddle coat. Delta is 3 years old. This is a Tamaskan dog. The Tamaskan's name is Whiskey. Whiskey has a nice wolf grey coat. Whiskey is 2 years old. It is now possible to do dynamic things with each |
Assignments[edit | edit source] |
|
